case statement based on file availability


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting case statement based on file availability
# 1  
Old 05-29-2004
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.
# 2  
Old 05-29-2004
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 01:14 PM..
# 3  
Old 05-29-2004
Probably want to change the file test to -r which tests whether the file exists and is readable. My 2 cents anyway! Good Luck.
# 4  
Old 05-29-2004
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

# 5  
Old 05-29-2004
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 "
# 6  
Old 05-29-2004
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.
# 7  
Old 06-01-2004
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

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Homework & Coursework Questions

Case Statement

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hey, guys I really need some help with a project. "Write a shell program that examines the command line... (8 Replies)
Discussion started by: sk192010`
8 Replies

3. Shell Programming and Scripting

Case Statement

Hey, guys I really need some help with a project. "Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a - I know I have to use... (2 Replies)
Discussion started by: sk192010`
2 Replies

4. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

5. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

6. Shell Programming and Scripting

case statement

hi all i'm writing a script and in it i need to prompt the user if the entered value is correct or not ,i wrote the following and its not working ,its executing the script even if i enter Y/N pls any help is appreciated echo "\nAre you sure you entered the right Destination Environment? y :... (5 Replies)
Discussion started by: bkan77
5 Replies

7. Shell Programming and Scripting

How can I get an if statement to execute based on number of lines in a file?

I need to have an if statement in a script to run if there are certain processes running. Easiest way I can see to do this is to run a ps and grep the results based on what I am looking for: $ ps -ef | grep wtrs --- webtrend 5046 1 0 May 12 ? 0:28 /webtrends/versions/6.1/wtrs_ui... (6 Replies)
Discussion started by: LordJezo
6 Replies
Login or Register to Ask a Question