开发者

Is it possible to fill Select dialog in Linux Bash script with piping?

I am trying to fill a Select dialog in bash scripting with the filenames of the working directory.

Is it possible to do this:

ls | egrep 'somefilter' > foo.txt

dialog --backtitle "Linux Shell Script" --title "Main\
Menu" --menu "Move using [UP] [DOWN],[Enter] to\
Select" 15 50 3\
0 "line of foo.txt"
1 "2nd line of foo.txt"
..etc 2>/tmp/menuitem.$$

Update on answer!

IFS=$'\开发者_如何学编程n\t'
dialog --backtitle "Converter v0.1b" --title "Select File\
$(($counter+1))\\$na" --menu "Move using [UP] [DOWN],[Enter] to\    
Select" 15 50 8 $LINES     2>/tmp/menuitem.$$   

menuitem=`cat /tmp/menuitem.$$`

as above is looking right now, it correctly fills up the selection list, any idea on how to get selection number X (let's say 3 = movie.mkv).

Right now: menuitem = 3 (if I chose number 3 in the list).

Need to figure out how to get it to say movie.mkv. Tried a bunch, didn't work.


Something like this?

filter()
{
    NUM=0
    ls | egrep 'somefilter' | while read i ; do
        echo $NUM $i
        NUM=$(($NUM+1))
    done
}

LINES=$(filter)

dialog --backtitle "Linux Shell Script" --title "Main\
Menu" --menu "Move using [UP] [DOWN],[Enter] to\
Select" 15 50 3 $LINES 2>/tmp/menuitem.$$

(won't work if you have spaces in your filenames)

Edit: here is an ugly solution for spaces (well, it's a bash script...):

filter() 
{
    NUM=0
    ls | egrep 'somefilter' | while read i ; do
        echo $'\t'$NUM$'\t'$i
        NUM=$(($NUM+1))
    done
}

LINES=$(filter)

IFS=$'\n\t'

dialog --backtitle "Linux Shell Script" --title "Main\
Menu" --menu "Move using [UP] [DOWN],[Enter] to\
Select" 15 50 3 $LINES 2>/tmp/menuitem.$$

unset IFS

Edit2: Use this to retrieve the chosen filename:

ITEM=$(cat /tmp/menuitem.$$)
FILE=$(echo "$LINES" | sed -n 's/\t'$ITEM'\t//p')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜