Operating System - HP-UX
1829958 Members
2035 Online
109998 Solutions
New Discussion

scripts and case statement

 
SOLVED
Go to solution
Jeffrey W. Stewart
Frequent Advisor

scripts and case statement

I am trying to write a script to make the job of copying files a much easier task. Here is my attempt at the script. I would like the script to repeat, and stop when a command like "99" is entered.

#!/bin/sh
# An example with the case statement
# Reads a command from the user and processes it
echo "Enter your command (e.g. 724778:801)"
read command
case "$command" in
724778-801)
cp /hp3070/qm/pbq/$command/events A04940.txt
;;
724778:803)
cp /hp3070/qm/pbq/$command/events A04278.txt
;;
724782-801)
cp /hp3070/qm/pbq/$command/events A04615.txt
;;
99)
echo "exit"
?? (command to exit
;;
esac

5 REPLIES 5
Pete Randall
Outstanding Contributor
Solution

Re: scripts and case statement

It seems like you're looking for the command to make the script exit? It's "exit", as in:

"99" )
echo exiting...
exit 0;;

Pete
Steve Steel
Honored Contributor

Re: scripts and case statement

Hi


command=ok
while [ "$command" != "99" ]
do

your script

done



The do loop will repeat as long as command is not 99


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Muthukumar_5
Honored Contributor

Re: scripts and case statement

Use exit for that. By normal successful exit use exit 0 at the end of every shell script. To differentiate use exit 1 or some int. It will be good as like system commands.

And more if you are giving an unknown option then your script will not handle that one.

So use as like

*)
echo "Unknown options.. and your set of options"
exit 2

It will be very good on your script.

Muthukumar.

Easy to suggest when don't know about the problem!
Victor Fridyev
Honored Contributor

Re: scripts and case statement

Hi,

The following usually works fine:

while true; do
echo "Enter your command (e.g. 724778:801)"
read command
case $command in
...
...
99) break ;;
*) echo "Input error:<$command>" ;;
esac
done
Entities are not to be multiplied beyond necessity - RTFM
Jeffrey W. Stewart
Frequent Advisor

Re: scripts and case statement

Solution was provided and I am closing threads that have been open and have answers