![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linux Replicated High Availability Manager 1.4.2 (DRBD 8.2 based branch) | iBot | Software Releases - RSS News | 0 | 04-15-2008 11:00 AM |
| case statement | bkan77 | Shell Programming and Scripting | 5 | 09-11-2007 02:54 PM |
| How can I get an if statement to execute based on number of lines in a file? | LordJezo | Shell Programming and Scripting | 6 | 05-14-2004 07:50 AM |
| Case Statement | Zeta_Acosta | Shell Programming and Scripting | 19 | 04-06-2004 01:16 PM |
| case statement | Bab00shka | Shell Programming and Scripting | 1 | 07-15-2002 02:31 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
case statement based on file availability
I need to make a select menu that gives options dynamically based on whether certain files are available. For instance:
PS3="Open which readme? " select readme in This That do case "$readme" in This) open -a /Applications/TextEdit.app This.txt;; That) open -a /Applications/TextEdit.app That.txt;; * ) echo "Invalid selection";; esac done } The catch is that if the file doesn't exist, it should not show in the menu, either. I'm fairly new to shell scripting, so any help would be appreciated. |
| Forum Sponsor | ||
|
|
|
|||
|
Thanks for the example. I follow the logic for making a list of existing files, but it's the case statement that is confusing me. I'm not sure what to put in the 'options' section (talk about irony - that's where you stopped). This is the output (no options selectable):
1) This.txt 2) That.txt #? If I select either option, it returns Invalid Selection. Here's the code I used: Code:
i=0
for item in This.txt TheOther.txt That.txt
do
if [ -r $item ]; then
existing[i]=$item
let i++
fi
done
select readme in "${existing[@]}"
do
case "$readme" in
$existing[1]) echo "This.txt";;
$existing[2]) echo "Red Herring";;
$existing[3]) echo "That.txt";;
* ) echo "Invalid Selection";;
esac
done
|
|
||||
|
Well, I would go this....
Code:
i=0
for item in This.txt TheOther.txt That.txt
do
if [ -r $item ]; then
existing[i]=$item
let i++
fi
done
select readme in "${existing[@]}"
do
case "$readme" in
This.txt) echo "This.txt";;
TheOther.txt) echo "Red Herring";;
That.txt) echo "That.txt";;
* ) echo "Invalid Selection";;
esac
done
Google's right in that you need to use the braces. But an additional problem is that the array is zero based. The first subscript should be zero, not one. But switching to correct subscripts still leaves a problem. ${existing[0]} will be This.txt only if This.txt exists. That was the point of the test. If This.txt does not exist and TheOther.txt does exist, ${existing[0]} will be TheOther.txt. This is why I would go with just constants in the case statement. |
|
||||
|
You don't really need to complicate things by using loops and arrays...
Code:
PS3="Open which readme? "
select readme in $(ls This.txt That.txt 2> /dev/null)
do
case "$readme" in
*.txt) echo open -a /Applications/TextEdit.app "$readme"; break;;
*) echo "Invalid selection";;
esac
done
|
||||
| Google The UNIX and Linux Forums |