Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
Any chance someone could show me some code to generate a menu based on the contents of a directory.
I have had a few attempts but cannot figure out how to make it generate the case statments on the fly.
I thought of perhaps generating a script by using cat into a file but this seems rather cumbersome, any one got any better ways to do this?
Rebooting is a sign of Weakness!

Have you considered using the select keyword:
#!/bin/bash
PS3="select an item ..."
select object in `ls -1`
do
case "$object" in
file1) echo "file1";;
file2) echo "file2";;
file3) echo "file3";;
*) echo "invalid";;
esac
done

Thanks for your reply nails,
I managed to produce the same output as you mentioned but could not workout how to make the case staments work.
ie if I choose number 1 then whatever is number is executed.
ie
#!/bin/bash
PS3="select an item ..."
select object in `ls -1`
do
case "$object" in
$number) echo "$file1";;
$number) echo "$file2";;
$number) echo "$file3";;
*) echo "invalid";;
esac
done$number would need to increase in value from 1 to however many files are in theat directory.
$file would need to match the $number entryIf anyone has any idea how this can be achived that would be great.
Rebooting is a sign of Weakness!

You might try this, but I can't guarantee the flexiblity of the solution:
#!/bin/ksh
menu_str=`ls -1 file*`
#echo $menu_str# build an array and the file variables
cnt=0
for i in $menu_str
do
menu_item[cnt]=${i}
eval ${i}=\${i}
((cnt=$cnt+1))
doneselect object in $(echo "${menu_item[*]}")
do
case "$object" in
$file1) echo "file1";;
$file2) echo "file2";;
$file3) echo "file3";;
*) echo "invalid";;
esac
done

Thanks again nails for your help.
I ended up with this script which will require a little work but does what I am after. Thought someone may also find it usefull.
ROOTDIR="/proc/acpi"
TMOUT=60
PS3='Pick one of the above: '
select i in `ls $ROOTDIR`
do
PS3='Choose Action: '
select a in "list" "print" "edit" "quit"
do
case "$a" in
list ) ls $ROOTDIR/$i;break ;;
print ) cat $ROOTDIR/$i;break;;
edit ) ${EDITOR-vi} $ROOTDIR/$i;break ;;
quit ) break ;;
"") echo you must select one of the above ;;
esac
done
exit 0
doneRebooting is a sign of Weakness!

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |