You need to end the statements under each condition with a double semicolon (;;
). If you do not, the statements under the next condition will also be executed.
The last condition should be default
and is executed if none of the other conditions is met. For each of the specified conditions, all the associated statements until breaksw
are executed.
The break
and exit
Statements
You should be aware of two other statements: the break
statement and the exit
statement.
The break
statement can be used to terminate an iteration loop, such as a for
, until
, or repeat
command.
exit
statements can be used to exit a shell program. You can optionally use a number after exit
. If the current shell program has been called by another shell program, the calling program can check for the code (the $?
or $status
variable, depending on the shell) and make a decision accordingly.
Using Functions in Shell Scripts
As with other programming languages, shell programs also support functions. A
The following is the format of a function in bash
:
func() {
}
You can call a function as follows:
func
The parameters
,
, and so on are optional. You can also pass the parameters as a single string — for example, $@
. A function can parse the parameters as if they were positional parameters passed to a shell program from the command line as command-line arguments, but instead use values passed inside the script. For example, the following script uses a function named Displaymonth()
that displays the name of the month or an error message if you pass a month number out of the range 1 to 12. This example works with bash
:
#!/bin/sh
Displaymonth() {
case $1 in
01 | 1) echo 'Month is January';;
02 | 2) echo 'Month is February';;
03 | 3) echo 'Month is March';;
04 | 4) echo 'Month is April';;
05 | 5) echo 'Month is May';;
06 | 6) echo 'Month is June';;
07 | 7) echo 'Month is July';;
08 | 8) echo 'Month is August';;
09 | 9) echo 'Month is September';;
10) echo 'Month is October';;
11) echo 'Month is November';;
12) echo 'Month is December';; *)
*) echo 'Invalid parameter';;
esac
}
Displaymonth 8
The preceding program displays the following output:
Month is August
Reference
> http://www.gnu.org/software/bash/bash.html — The bash
home page at the GNU Software Project.
> http://www.tldp.org/LDP/abs/html/ — Mendel Cooper's 'Advanced Bash-Scripting Guide.'
> http://www.linuxnewbie.org/nhf/intel/shells/basic.html — Learn basic shell commands at this site.
> http://www.cs.princeton.edu/~jlk/lj/ — 'The New Korn Shell — ksh93,' an article by David G. Korn, Charles J. Northrup, and Jeffery Korn regarding the korn
shell.
> http://lug.umbc.edu/~mabzug1/bash-httpd.html — The Bash-httpd FAQ, with links to the latest version of the bash
web server, bash-httpd-0.02
.
> http://www.tcsh.org/ — Find out more about tcsh
here.
> http://www.zsh.org/ — Examine zsh
in more detail here.
CHAPTER 34
Advanced Software Management
Fedora is provided with a large number of packages already supplied and ready to go. However, Fedora also comes with a great package management tool, called yum
, which enables you to quickly and easily download and install more packages. The yum
tool (Yellowdog Updater, Modified) has been included with Fedora since version 1 and has become the tool of choice for installing and updating applications. This chapter takes a look at how yum works, its basic usage, and more advanced package and repository management, including using some of the GUI tools available with Fedora. But before that, we take a look at RPM, which is the underlying technology that yum
utilizes.
Using RPM for Software Management