Cases

While you are already looking at /etc/init.d for the previous example, it is a good time to note that the i nit scripts in /etc/init.d are a good place to see examples of the use of the case statement. The case statement provides a way of choosing what to do based on a particular decision. This simple example case.sh is enough to illustrate the principle:

echo "What a nice day it is today" sad)

echo "What a gloomy day it is today"

esac

So the script is deciding what it will output on the basis of the value of a variable (in this case the argument that was given). Such conditional branching is a key feature of any kind of programming:

[email protected]:~ > ./case.sh happy What a nice day it is today [email protected]:~ > ./case.sh sad What a gloomy day it is today [email protected]:~ > ./case.sh mad [email protected]:~ >

Notice that the alternative tests are each followed by a right-hand parenthesis, and the conditional block as a whole is terminated by a double semicolon. The whole set of conditions is closed by esac.

Was this article helpful?

0 0

Post a comment