Computing.Net > Forums > Linux > Shell Script Menu help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Shell Script Menu help

Reply to Message Icon

Name: pobman
Date: April 29, 2006 at 13:46:13 Pacific
OS: FEDORA 5
CPU/Ram: AMD2000+ / 512MB
Product: AMD
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 1, 2006 at 08:28:52 Pacific
Reply:

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


0

Response Number 2
Name: pobman
Date: May 2, 2006 at 02:22:29 Pacific
Reply:

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 entry

If anyone has any idea how this can be achived that would be great.

Rebooting is a sign of Weakness!


0

Response Number 3
Name: nails
Date: May 3, 2006 at 10:05:06 Pacific
Reply:

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))
done

select object in $(echo "${menu_item[*]}")
do
case "$object" in
$file1) echo "file1";;
$file2) echo "file2";;
$file3) echo "file3";;
*) echo "invalid";;
esac
done


0

Response Number 4
Name: pobman
Date: May 3, 2006 at 12:33:18 Pacific
Reply:

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
done

Rebooting is a sign of Weakness!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Shell Script Menu help

HELP with Shell Scripting www.computing.net/answers/linux/help-with-shell-scripting-/19953.html

advanced shell script help www.computing.net/answers/linux/advanced-shell-script-help/17749.html

Shell Script problem..need help! www.computing.net/answers/linux/shell-script-problemneed-help/22814.html