The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-29-2004
Registered User
 

Join Date: Jun 2003
Posts: 20
Stumble this Post!
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-29-2004
Registered User
 

Join Date: May 2004
Location: Hawaii
Posts: 37
Stumble this Post!
i=0
for item in This That
do
if [ -e $item ]
then
existing[i]=$item
let i++
fi
done

select readme in "${existing[@]}"
do
case "$readme" in
...

Last edited by anarchie; 05-29-2004 at 09:14 AM.
Reply With Quote
  #3 (permalink)  
Old 05-29-2004
google's Avatar
Moderator
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Stumble this Post!
Probably want to change the file test to -r which tests whether the file exists and is readable. My 2 cents anyway! Good Luck.
Reply With Quote
  #4 (permalink)  
Old 05-29-2004
Registered User
 

Join Date: Jun 2003
Posts: 20
Stumble this Post!
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
Reply With Quote
  #5 (permalink)  
Old 05-29-2004
google's Avatar
Moderator
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Stumble this Post!
Code:
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

try changing the options to this (i.e. - add braces)

                ${existing[1]})	echo "This.txt";;
		${existing[2]})	echo "Red Herring";;
		${existing[3]})	echo "That.txt";;
Also, at the begining of your script add the following line:
PS3= "Select An Option From the List Above "
Reply With Quote
  #6 (permalink)  
Old 05-29-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,426
Stumble this Post!
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
So I would put one entry in the case statement for each item in the "for" loop. I don't see any value in using the array syntax.

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.
Reply With Quote
  #7 (permalink)  
Old 06-01-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,242
Stumble this Post!
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0