QuickStart Shell Scripting (iv)

Source: Internet
Author: User
Tags case statement

Lee produced, reproduced please specify the source http://blog.csdn.net/hnulwt/article/details/43234977

Continue with what you learned last time, this time studying case statements and the function concepts of the shell.

Case statement

First look at the test program:

  1 #! /bin/sh  2  3 Animal=dog  4  5 case "$animal" in  6 cat)  7         echo "Cat";;  8 dog)  9         echo "Dog", "Ten Lion"         echo "lion"; Esac

The above program operation result is very simple, output: Dog

His structure is:

Case "variable name" in

String one)

Execute statement one;;

String two)

Execute statement two;;

Esac

Some of the points that require special attention are:

The variable name after the 1,case is enclosed in quotation marks (line 5th in the above example program)

2, matching criteria, the string is preceded by parentheses, followed by parentheses (6, 8, 10 lines in the program above)

3, a double semicolon is needed to execute the statement end position; (7, 9, 11 lines in the program above)

4, ending with an inverted case match, i.e. ESAC

Function

First we look at one of the simplest functions, the following is a program:

  1 #! /bin/sh  2  3 Saydog ()  4 {  5         echo "Dog"  6         return $?  7}  8  9 Saydog
The program is simple, 3-7 lines we define the Saydog function. Row six actually can also ignore, $? The return value of the function is described in the previous section, and in line 9th we call this function.

To see the results of the program running:

$ sh test.sh
Dog

Parameters

How to pass parameters inside the function, the shell is not like we write Java, C + + program in parentheses to pass parameters, pass parameters only need to directly inside the function using $n can (n represents the number of parameters passed in)

Look at the following procedure:

  1 #! /bin/sh  2  3 sayanimals ()  4 {  5         echo $  6         echo $  7}  8  9 sayanimals Cat Dog
In line 5th and 6 of the above function, the passed parameter 1 and parameter 2 are used. The 9th line calls the function and passes in the cat dog two parameters, and we take a look at the result:

$ sh test.sh
Cat
Dog

Did you see that? The shell incoming parameters are actually very simple.
return value
The return value of the shell is different from the normal program return value that we understand, and the Shell function return value can only be a numeric value, usually to indicate success or failure of the function, 0 for success, and other values to fail. Therefore, it is inappropriate to return the function execution result with the return value of the function. If you return a string, you get an error message: "Numeric argument required"

The program is dead, people are alive, that's not good, we can do it by defining a global variable, the function assigns the result of the calculation to the global variable, and then accesses the global variable elsewhere in the script.

Input and output redirection

People who have used tools such as ant may not be familiar with this concept.

Each unix/linux command will open three files when it is run:

Standard input file (stdin): stdin file descriptor for 0,unix program reads data from stdin by default.

Standard output file (stdout): StdOut file descriptor for 1,unix program outputs data to stdout by default.

Standard error file (stderr): The stderr file descriptor for the 2,unix program writes an error message to the stderr stream.

The shell redirection is used to redirect standard output or standard error output to a different location.

Standard input, standard output, what is the standard error output? We can understand that: the keyboard is the standard input, the monitor is the standard output, the error output is still the display by default.

Redirection is the thing that will be output on the display, directed to other locations, such as the file.

The syntax for the output redirection is: directives > file names

For example, we execute command ls > test at the terminal, when we don't see the output on the screen, LS is the file in the current directory, and we have redirected the output to a file called test. We open with VIM test and we see the results. It is true that the result is output to the test file. No longer pictured here, execute your own commands.

Output redirection also has a format: directives >> file names

This format guarantees that if a file already exists, the result is appended to the end of the file, which is not overwritten.

Syntax for input redirection: directives < file names

Example: For example, we have a file Test.txt with 3 lines of content. We want to count his number of rows, which can be counted by executing commands: Wc-l < Test.txt.

Result output: 3

Well, through the content of section four, we have mastered the basic use of the shell, can write some simple shell program, but there is a gap between the real coding, so we have to continue to work! More practice, read more about the shell program written by others, to achieve learning, improve their own purposes.

QuickStart Shell Scripting (iv)

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.