Shell programming Basics

Source: Internet
Author: User
Tags binary to decimal chop echo command

We can use any text editor, such as gedit, kedit, emacs, VI, to write shell scripts. It must start with the following line (the first line of the file must be placed ):

 
#! /Bin/sh...

Note: It is best to use "! /Bin/bash "instead of"! /Bin/sh ", if you use TC shell to change to tcsh, others are similar.

Symbol #! Used to tell the system to execute the scriptProgramIn this example, use/bin/sh. After editing and saving, if you want to execute the script, you must first make it executable:

 
Chmod + x filename

Enter./filename in the directory where the script is located to run the script.

Variable assignment and reference

In shell programming, variables do not need to be declared in advance, and the naming of variable names must follow the following rules:

    1. The first character must be a letter (a-Z, A-Z)
    2. There must be no space in the middle. You can use underscores (_)
    3. Punctuation cannot be used.
    4. You cannot use keywords in bash (you can use the help command to view reserved keywords)

When you need to assign a value to a variable, you can write it like this:

 
Variable name = Value

To use the value of a variable, you only need to add $ before the variable name (NOTE: When assigning values to the variable, no space is left on both sides of "=)

#! /Bin/sh # assign a value to the variable: A = "Hello World" # No space exists on both sides of the equal sign # print the value of variable A: Echo "A is:" $

Select your favorite editor, input the above content, save it as the file first, execute chmod + X first to make it executable, and finally input./first to execute the script. The output result is as follows:

 
A is: Hello World

Sometimes the variable name may be confused with other words, such:

 
Num = 2 Echo "this is the $ numnd"

The above script does not output "this is the 2nd" but "this is the"; this is because shell will search for the value of the variable numnd, but in fact this variable does not have a value at this time. In this case, we can use curly brackets to tell shell to print the num variable:

 
Num = 2 Echo "this is the $ {num} Nd"

The output result is: This is the 2nd

Note that the default shell value assignment is a string value assignment. For example:

 
Var = 1var = $ var + 1 echo $ VaR

The printed result is not 2 but 1 + 1. There are several expressions to achieve the desired effect:

 
Let "Var + = 1" Var = $ [$ var + 1] Var = 'expr $ var + 1' # note the spaces on both sides of the plus sign. Otherwise, the values are assigned as strings.

Note: The first two methods are valid in bash, and errors may occur in Sh.

Let indicates a mathematical operation. expr is used for integer calculation. each item is separated by spaces. $ [] uses the expressions in brackets as mathematical operations to calculate the results before outputting them.

Many variables are automatically set in shell scripts. We will explain them when using these variables. In addition to common shell variables that are valid only in the script, there are also environment variables, that is, those that have been processed by the Export keyword. This article does not discuss environment variables, because they are generally used only in login scripts.

Process Control in Shell

If sentence

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

 
If...; then... Elif...; then... else... 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:

 
#! /Bin/shif [$ {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 can compare it with/bin/Bash to determine whether the current shell is Bash.

& | 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:

 
#! /Bin/shmailfolder =/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, 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.

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

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:

 
#! /Bin/sh FTYPE = 'file "$1" '# Note' and 'is different 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 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.

Select statement

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

 
Select VaR in...; do break; done ...... now $ VaR can be used ....

The following is a simple example:

 
#! /Bin/shecho "What is your favorite OS? "Select VaR in" Linux "" GNU Hurd "" Free BSD "" other "; do break; doneecho" you have selected $ Var"

If the preceding script appears, select: Not found will be #! /Bin/sh #! /Bin/Bash:

 
What is your favorite OS? 1) linux2) GNU hurd3) Free bsd4) Other #? 1you have selected Linux

While/For Loop

In shell, you can use the following loop:

 
While...; do... 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 row table (strings are separated by spaces) and assign it to a variable:

For VaR in...; do... Done

The following example prints a B c to the screen:

 
#! /Bin/shfor var in a B C; do echo "Var is $ Var" done

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

 
#! /Bin/sh # list a Content summary of a number of RPM packages # usage: showrpm rpmfile1 rpmfile2... # example: showrpm/CDROM/RedHat/RPMS /*. rpmfor rpmpackage in $ *; do if [-R "$ rpmpackage"]; then ECHO "=======================$ rpmpackage ==================" rpm-qi-P $ rpmpackage else echo "error: cannot read file $ rpmpackage "fidone

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.

Some special symbols in Shell

Quotation marks

Before passing any parameters to a program, the program extends the wildcards and variables. Here, the so-called extension means that the program will replace the wildcard (for example, *) with the appropriate file name, and replace the variable with the variable value. We can use quotation marks to prevent this extension. Let's look at an example. Suppose there are two JPG files in the current directory: mail.jpgand tux.jpg.

 
#! /Bin/shecho *. jpg

The running result is:

 
Mail.jpg tux.jpg

Quotation marks (single quotation marks and double quotation marks) can prevent wildcard * extension:

 
#! /Bin/shecho "*. jpg" Echo '*. jpg'

The running result is:

 
*. Jpg *. jpg

The single quotation marks are more strict, which can prevent any variable extension. The double quotation marks can prevent wildcard extension but allow variable extension:

 
#! /Bin/shecho $ shellecho "$ shell" Echo '$ shell'

The running result is:

 
/Bin/bash $ Shell

In addition, there is also a method to prevent such expansion, that is, to use the Escape Character -- backslice bar :/:

 
Echo/*. jpgcho/$ Shell

Output result:

 

 

 
*. Jpg $ Shell

Here document

When you want to pass several lines of text to a command, using here statements is a good method. It is very useful to write a helpful text for each script. If you use here statements, you do not need to use the echo function to output a row. Here document starts with <, followed by a string, which must also appear at the end of the here document. The following is an example. In this example, we rename multiple files and print the help using here documents:

 
#! /Bin/sh # We have less than 3 arguments. print the help text: If [$ #-lt 3]; thencat  

This example is a bit complicated. We need to spend more time to illustrate it. The first if expression determines whether the number of input command line parameters is smaller than 3 (special variable $ # indicates the number of parameters included ). If the number of input parameters is less than three, the help text is passed to the cat command and then printed on the screen by the cat command. Print the help text and exit the program. If the input parameter is equal to or greater than three, we assign the first parameter to the variable old, and the second parameter to the variable new. Next, we use the shift command to delete the first and second parameters from the parameter list, so that the original third parameter becomes the first parameter in the parameter list $. Then we start the loop. The command line parameter list is assigned to the variable $ file one by one. Then we determine whether the file exists. If yes, we use the SED command to search for and replace the file to generate a new file name. Then, assign the command result in the backslash to newfile. In this way, the old and new file names are obtained. Rename the file using the MV command.

Functions in Shell

If you have written complex scripts, you may find that the sameCodeIn this case, using the function is much more convenient. The functions are roughly as follows:

 
Functionname () {# inside the body $1 is the first argument given to the function #$2 the second... body}

You need to declare the function at the beginning of each script.

The following is a script named xtitlebar, which can change the terminal window name. Here we use a function named help, which is used twice in the script:

#! /Bin/sh # VIM: Set Sw = 4 ts = 4 et: Help () {cat  

It is a good programming habit to help other users (and themselves) use and understand scripts.

= Command line parameter = xxxxxxxxxxxxxxxxxxxxxxxxxx

We have seen Special variables such as $ * and $1, $2... $9. These special variables include the parameters you input from the command line. So far, we have only learned some simple command line syntax (such as some mandatory parameters and the-H option for viewing help ). However, when writing more complex programs, you may find that you need more custom options. The common practice is to add a minus sign before all optional parameters, followed by a parameter value (such as a file name ).

There are many ways to analyze input parameters, but the example using the case expression below is undoubtedly a good method.

#! /Bin/sh help () {cat  

You can run the script like this:

 
Extends parser-l Hello-f ---somefile1 somefile2

The returned results are as follows:

 
Opt_f is 1opt_l is hellofirst Arg is-somefile12nd Arg is somefile2

How does this script work? The script first loops through all input command line parameters and compares the input parameters with the case expression. If the input parameters match, a variable is set and the parameter is removed. According to the Convention of the UNIX system, the first input should be the parameter containing the minus sign.

Shell script example

=== General programming steps === xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Now let's discuss the general steps for writing a script. Any excellent script should have help and input parameters. It is a good idea to write a framework script (Framework. Sh) that contains the framework structure required by most scripts. In this way, when writing a new script, you can first execute the following command:

 
CP framework. Sh myscript

Then insert your own function.

Let's take a look at the two examples below.

Binary to decimal conversion

The script b2d converts the binary number (such as 1101) to the corresponding decimal number. This is also an example of a mathematical operation using the expr command:

#! /Bin/sh # VIM: Set Sw = 4 ts = 4 et: Help () {cat 

TheAlgorithmIt uses decimal and binary values (, 16,...). For example, binary "10" can be converted to decimal:

 
0*1 + 1*2 = 2

To obtain a single binary number, we use the lastchar function. This function uses WC-C to calculate the number of characters, and then uses the cut command to retrieve the last character. The Chop function removes the last character.

Circular file copy

You may want to do this all the time: Save all sent emails to a file. However, after a few months, the file may become so large that the access speed of the file may be slow. The following script rotatefile can solve this problem. This script can rename the email storage file (assuming outmail) to outmail.1, and the original outmail.1 is changed to outmail.2 and so on...

 #! /Bin/sh # VIM: Set Sw = 4 ts = 4 et: vers = "0.1" Help () {cat 
  

How does this script work? After detecting that a file name is provided by the user, perform a loop from 9 to 1. Rename file name. 9 to file name. 10, and rename file name. 8 to file name. 9 ...... And so on. After the loop ends, name the original file as file name. 1 and create an empty file (touch $ filen) with the same name as the original file)

Script debugging

The simplest debugging method is to use the echo command. You can use echo to print variable values wherever possible, which is why most shell Programmers spend 80% of their time debugging. The benefit of shell scripts is that it does not take much time to insert an echo command without recompilation.

Shell also has a real debugging mode. If the script "strangescript" has an error, run the following command to debug it:

 
Sh-x strangescript

The preceding command executes the script and displays the values of all variables.

Shell also has a mode to check the syntax without executing the script. The command is as follows:

 
Sh-N your_script

This command returns all syntax errors.

We hope that you can now write your own shell scripts and enjoy it! :)

 

Reprinted statement:This article from http://wiki.ubuntu.org.cn/Shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80

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.