Process Control in Shell

Source: Internet
Author: User
Tags case statement

1. If statement

If the "if" expression is true, the part after then is executed:
Toggle line numbers

1 If...; then
2 ....
3 Elif...; then
4 ....
5 else
6 ....
7 fi

In most cases, you can use test commands to test conditions, such as comparing strings, determining whether a file exists, and whether the file is readable ...... Generally, "[]" is used for conditional testing. Note that spaces are important. Make sure that the spaces before and after square brackets are used.

[-F "somefile"]: determines whether it is a file.
[-X "/bin/ls"]: determines whether/bin/ls exists and has the executable permission.
[-N "$ Var"]: determines whether the $ var variable has a value.
["$ A" = "$ B"]: determines whether $ A and $ B are equal.

Run man test to view all types of test expressions that can be compared and judged. The following is a simple if statement:
Toggle line numbers

1 #! /Bin/sh
2
3 if ["$ shell" = "/bin/bash"]; then
4 echo "your login shell is the bash (Bourne again shell )"
5 else
6 echo "your login shell is not bash but $ shell"
7 fi

The variable $ shell contains the name of the logon shell. We can compare it with/bin/Bash to determine whether the current shell is Bash.
2. & | Operator

If you are familiar with C, you may like the following expressions:

[-F "/etc/shadow"] & Echo "this computer uses shadow passwords"

& Here is a shortcut operator. If the expression on the left is true, execute the Statement on the right. You can also regard it as a logical operation and operation. The above script indicates that if the/etc/shadow file exists, print "this computer uses shadow passwords ". You can also use or operate (|) in shell programming, for example:
Toggle line numbers

1 #! /Bin/sh
2
3 mailfolder =/var/spool/mail/James
4 [-R "$ mailfolder"] | {echo "can not read $ mailfolder"; Exit 1 ;}
5 echo "$ mailfolder has mail from :"
6 grep "^ From" $ mailfolder

The script first checks whether mailfolder is readable. If it is readable, it prints a line of "from" in the file. If it is not readable or the operation takes effect, print the error message and exit the script. Note that the following two commands must be used:

-Print the error message.

-Exit the program.

We use curly braces to put the two commands together as one command in the form of an anonymous function. The common function will be explained later. We can use the if expression to accomplish anything without the use of and or operators, but the use of and or operators is much more convenient.
3. Case statement

The case expression can be used to match a given string, not a number (do not confuse switch... case in C ).

Case... in
...) Do something here ;;
Esac

Let's take an example. The file command can identify the file type of a given file, for example, file lf.gz. The output result is:

Lf.gz: gzip compressed data, deflated, original filename,
Last modified: Mon Aug 27 23:09:18 2001, OS: Unix

We use this to write a script named smartzip, which can automatically decompress Bzip2, Gzip, and zip compressed files:
Toggle line numbers

1 #! /Bin/sh
2
3 FTYPE = 'file "$1 "'
4 case "$ FTYPE" in
5 "$1: ZIP Archive "*)
6 unzip "$1 ";;
7 "$1: gzip COMPRESSED "*)
8 gunzip "$1 ";;
9 "$1: Bzip2 COMPRESSED "*)
10 bunzip2 "$1 ";;
11 *) error "File $1 can not be uncompressed with smartzip ";;
12 esac
Error: EOF in multi-line statement

You may notice that a special variable $1 is used above, which contains the first parameter value passed to the script. That is, when we run:

Smartzip articles.zip

$1 is the string articles.zip.
4. SELECT statement

The Select expression is an extended application of Bash and is good at interactive scenarios. You can select from different values:
Toggle line numbers

1 select VaR in...; do
2 break
3 done
4 ...... now $ VaR can be used ....

The following is a simple example:
Toggle line numbers

1 #! /Bin/sh
2
3 Echo "What is your favorite OS? "
4 select VaR in "Linux" "GNU Hurd" "Free BSD" "other"; do
5 Break
6 done
7 Echo "you have selected $ Var"

The script runs as follows:

What is your favorite OS?
1) Linux
2) GNU Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux

5. While/For Loop

In shell, you can use the following loop:
Toggle line numbers

1 While...; do
2 ....
3 done

As long as the test expression condition is true, the while loop will continue to run. The keyword "break" is used to jump out of the loop, while the keyword "continue" can skip the rest of the loop and directly jump to the next loop.

The for loop will view a string list (strings are separated by spaces) and assign it to a variable:
Toggle line numbers

1 For VaR in...; do
2 ....
3 done

The following example prints a B c to the screen:
Toggle line numbers

1 #! /Bin/sh
2
3 For var in a B C; do
4 echo "Var is $ Var"
5 Done

The following is a practical script showrpm. Its function is to print the statistics of some RPM packages:
Toggle line numbers

1 #! /Bin/sh
2
3 # list a Content summary of a number of RPM packages
4 # usage: showrpm rpmfile1 rpmfile2...
5 # example: showrpm/CDROM/RedHat/RPMS/*. rpm
6 For rpmpackage in $ *; do
7 if [-R "$ rpmpackage"]; then
8 Echo "=======================$ rpmpackage =================="
9 rpm-qi-p $ rpmpackage
10 else
11 echo "error: cannot read file $ rpmpackage"
12 fi
13 done

The second special variable $ * is displayed, which contains all the input command line parameter values. If you run showrpm OpenSSH. RPM w3m. RPM webgrep. rpm, $ * contains three strings: OpenSSH. rpm, w3m. rpm, and webgrep. rpm.

Related Article

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.