Linux shell script basic learning (3)

Source: Internet
Author: User

Linux shell script basic learning Lecture 3: When we introduced shell commands and Process Control in the previous section, we could not talk about process control due to space limitations, today, we will only introduce the first three parts of if case and select. The following three parts can only be introduced in the fourth lecture of Linux shell script basics.

1.1.5 Shell command and Process Control 2)

3) Process Control

1. if

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

If...; then

....

Elif...; then

....

Else

....

Fi

In most cases, you can use test commands to test the conditions. For example, you can compare strings, determine whether a file exists, and whether the file is readable...

"[]" Is usually used to represent a conditional test. Note that spaces are important. Make sure that the square brackets have spaces.

[-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.

Directly execute the following script:

#! /Bin/sh

If ["$ SHELL" = "/bin/bash"]; then

Echo "your login shell is the bash (bourne again shell )"

Else

Echo "your login shell is not bash but $ SHELL"

Fi

The variable $ SHELL contains the name of the logon shell. We compared it with/bin/bash.

Shortcut Operators

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

[-F "/etc/shadow"] & echo "This computer uses shadow passwors"

Here & is a shortcut operator. If the expression on the left is true, execute the Statement on the right.

You can also think of it as a logical operation. In the above example, if the/etc/shadow file exists, print "This computer uses shadow passwors ". Similarly, operations (|) are also available in shell programming. Here is an example:

#! /Bin/sh

Mailfolder =/var/spool/mail/james

[-R "$ mailfolder"] ''{echo" Can not read $ mailfolder "; exit 1 ;}

Echo "$ mailfolder has mail from :"

Grep "^ From" $ mailfolder

The script first checks whether mailfolder is readable. If it is readable, the "From" line in the file is printed. If it is not readable or the operation takes effect, print the error message and exit the script. There is a problem here, that is, we must have two commands:

-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. General functions will be mentioned below.

We can use the if expression to do anything without the sum or operator, but it is much more convenient to use the sum or operator.

2. case

Case: The expression can be used to match a given string, not a number.

Case... in

...) Do something here ;;

Esac

Let's look at an example. The file command can identify the file type of a given file, for example:

File lf.gz

This will return:

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:

#! /Bin/sh

Ftype = 'file "$1 "'

Case "$ ftype" in

"$1: Zip archive "*)

Unzip "$1 ";;

"$1: gzip compressed "*)

Gunzip "$1 ";;

"$1: bzip2 compressed "*)

Bunzip2 "$1 ";;

*) Echo "File $1 can not be uncompressed with smartzip ";;

Esac

You may notice that we use a special variable $1 here. The variable contains the first parameter value passed to the program.

That is, when we run:

Smartzip articles.zip

$1 is the string articles.zip

3. selsect

The select expression is a bash extension application, especially for interactive use. You can select from a group of different values.

Select var in...; do

Break

Done

... Now $ var can be used ....

The following is an example:

#! /Bin/sh

Echo "What is your favorite OS? "

Select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do

Break

Done

Echo "You have selected $ var"

The following is the result of running the script:

What is your favorite OS?

1) Linux

2) Gnu Hurd

3) Free BSD

4) Other

#? 1

You have selected Linux

The above is the content of this lecture. There are many control procedures. Here we will first introduce these three.

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.