Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

The case Command

Often you'll find yourself trying to evaluate the value of a variable, looking for a specific value within a set of possible values. In this scenario, you end up having to write a lengthy if-then-else statement, like this:

$ cat test25

#!/bin/bash

# looking for a possible value

 

if [ $USER = “rich” ]

then

   echo “Welcome $USER”

   echo “Please enjoy your visit”

elif [ $USER = barbara ]

then

   echo “Welcome $USER”

   echo “Please enjoy your visit”

elif [ $USER = testing ]

then

   echo “Special testing account”

elif [ $USER = jessica ]

then

   echo “Do not forget to logout when you're done”

else

   echo “Sorry, you are not allowed here”

fi

$ ./test25

Welcome rich

Please enjoy your visit

$

The elif statements continue the if-then checking, looking for a specific value for the single comparison variable.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint