Linux Shell -- case statement

Source: Internet
Author: User
The case statementthe case statement is good alternative to multilevel if-then-else-fi statement. It enable you to match several
Values against one variable. Its easier to read and write. Syntax:

           case  $variable-name  in                pattern1)   command                                ...                                ..                                command;;                pattern2)   command                                ...                                ..                                command;;                patternN)   command                                ...                                ..                                command;;                *)             command                                ...                                ..                                command;;           esac

The $ variable-name is
Compared against the patterns until a match is found. the shell then executes all the statements up to the two semicolons that are next to each other. the default is *) and its executed if no match is found. for e.g. write script as follows:
#
if no vehicle name is given
# i.e. -z $1 is defined and it is NULL
#
# if no command line arg

if [ -z $1 ]
then
  rental="*** Unknown vehicle ***"
elif [ -n $1 ]
then
# otherwise make first arg as rental
  rental=$1
fi

case $rental in
   "car") echo "For $rental Rs.20 per k/m";;
   "van") echo "For $rental Rs.10 per k/m";;
   "jeep") echo "For $rental Rs.5 per k/m";;
   "bicycle") echo "For $rental 20 paisa per k/m";;
   *) echo "Sorry, I can not gat a $rental for you";;
esac

First script will check, that if $1 (First Command Line argument) is given or not, if not given set value of Marshal
Variable to "*** unknown vehicle ***", if command line Arg is supplied/Given Set Value of variable Al variable to given value (command line Arg ). the $ specified Al is compared against the patterns until a match is found.
For first test run its match with Van and it will show output"
Van Rs.10 per K/M ."
For second test run it print,"
Car Rs.20 per K/m ".
And for last run, there is no match for Maruti-800, hence default I. e. *) is executed and it prints, "Sorry,
I can not gat A Maruti-800 for you ".
Note that
Esac is always required to indicate end of case statement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.