1848939 Members
7214 Online
104039 Solutions
New Discussion

script: case

 
SOLVED
Go to solution
andrea_53
Advisor

script: case

Hi,
I need to do a script using case.
(case ... in )
Could someone give me an example?

thank you
3 REPLIES 3
Ross Zubritski
Trusted Contributor

Re: script: case

Here is some example case usage that we use to test conditions for a backup:

# TEST FOR CORRECT USAGE:
# # Ensure root is executing the script
UID=`/usr/bin/id -u` # Test the user id
case "$UID" in # Is it root?
0) # Do nothing user is root
;; # Otherwise...
*) echo "$BELL" # Ring the bell
echo "$USAGE" # Display the usage message
exit 1; # Abort the script
esac
# # Test the number of arguments
case "$#" in # How many arguments passed?
1) # Do nothing this is correct
;; # Otherwise...
*) echo "$BELL" # Ring the bell
echo "$USAGE" # Display the usage message
exit 1;; # Abort the script
esac
# # Test the argument passed
case "$1" in # What is the argument set to?
-o|-O) ;; # Undocumented see below
-f|-F) MODE="Full";; # Set up for weekly full backup
-i|-I) MODE="Incremental";; # Set up for daily incremental backup
-m|-M) MODE="MonthEnd" # Set up for month end full backup
;; # Otherwise...
*) echo "$BELL" # Ring the bell
echo "$USAGE" # Display the usage message
exit 1;; # Abort the script
esac
Christian Gebhardt
Honored Contributor

Re: script: case

Hi

echo "input: \c"
read var
case "$var"
in
1)
echo 1;;
2)
echo 2;;
*)
echo "something else";;
esac


Chris
steven Burgess_2
Honored Contributor
Solution

Re: script: case

Hi

echo "would you like a drink Yy or Nn \c"
read ans
case $ans in

Y|y) echo "I'll put the kettle on"
;;
n|N) echo "Ok"
;;
*) echo " Please reply Yy Nn "
;;
esac


You can see the actions are dependant upon the replay of the question

The construct is straight forward but obviously gets more complicated as you require more actions to be performed

Have a look here for tonnes of reference areas

http://forums.itrc.hp.com/cm/QuestionAnswer/0,,0xbb5e7e990647d4118fee0090279cd0f9,00.html

HTH

Steve

take your time and think things through