Shell programming Basics
Leal
Authorization license:
- Create sharing agreement
- GNU Free Documentation License
Edited by: firehare, dbzhang800
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/bash...
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 #! The program used to tell the system to execute the script. This example uses/bin/bash. 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.
Directory [hide]
- 1 variable assignment and reference
- 2. Process Control in Shell
- 3 & | Operator
- 3.1 case statement
- 3.2 SELECT statement
- 3.3 While/For Loop
- 4. Some special symbols in Shell
- 5 here document
- 6. Functions in Shell
- 7. Command Line Parameters
- 8 shell script example
- 8.1 general programming steps
- 8.2 convert binary to decimal
- 8.3 circular file copy
- 9 script debugging
|
[Edit] 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:
- The first character must be a letter (a-Z, A-Z) or _
- There must be no space in the middle. You can use underscores (_)
- Other punctuation marks cannot be used.
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/bash # 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=2echo "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=2echo "this is the ${num}nd"
The output result is: This is the 2nd
Note the brackets:
num=2echo "this is the {$num}nd"
The output result is: this is the {2} Nd
Note that the default shell value assignment is a string value assignment. For example:
var=1var=$var+1echo $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 ++) Var = $ ($ var + 1 )) var = "$ (expr" $ Var "+ 1)" # We do not recommend using VAR = "'expr" $ Var "+ 1'" # It is strongly not recommended, note the spaces on both sides of the plus sign. Otherwise, the value is assigned as a string, which is 'Below ESC, not a single quote '.
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.
[Edit] Process Control in Shell
[Edit] If clause
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/bashif [ ${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.
[Edit] & | 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, "this computer uses shadow passwords" is printed ". You can also use or operate (|) in shell programming, for example:
#!/bin/bashmailfolder=/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.
[Edit] 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/bash 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 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.
[Edit] 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/bashecho "What is your favourite OS?"select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do break;doneecho "You have selected $var"
The script runs as follows:
What is your favourite OS?1) Linux2) Gnu Hurd3) Free BSD4) Other#? 1You have selected Linux
[Edit] 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 list (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/bashfor 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/bash# list a content summary of a number of RPM packages# USAGE: showrpm rpmfile1 rpmfile2 ...# EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpmfor rpmpackage in "[email protected]"; do if [ -r "$rpmpackage" ];then echo "=============== $rpmpackage ==============" rpm -qi -p $rpmpackage else echo "ERROR: cannot read file $rpmpackage" fidone
The second special variable [email protected] appears, which contains all input command line parameter values. If you run showrpm OpenSSH. rpm w3m. rpm webgrep. rpm, then "[email protected]" (with quotation marks) contains three strings, namely, OpenSSH. rpm, w3m. rpm and webgrep. rpm. $ * Means similar. But there is only one string. If no quotation marks are added, parameters with spaces are truncated.
[Edit] some special symbols in Shell
[Edit] 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/bashecho *.jpg
The running result is:
mail.jpg tux.jpg
Quotation marks (single quotation marks and double quotation marks) can prevent wildcard * extension:
#!/bin/bashecho "*.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/bashecho $SHELLecho "$SHELL"echo ‘$SHELL‘
The running result is:
/bin/bash/bin/bash$SHELL
In addition, there is a method to prevent this extension, that is, to use the Escape Character -- backslice bar :\:
echo \*.jpgecho \$SHELL
Output result:
*.jpg$SHELL
[Edit] Here document
When you want to pass several lines of text to a command, using here document is a good method. It is very useful to write a helpful text for each script. If you use here document, 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. In this example, we rename multiple files and use the here document to print help:
#! /Bin/bash # We have less than 3 arguments. print the help text: If [$ #-lt 3]; thencat
The example is a little 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.
[Edit] functions in Shell
If you have written complicated scripts, you may find that the same code may be used in several places. In this case, it is much easier to use functions. The functions are roughly as follows:
functionname(){# inside the body $1 is the first argument given to the function# $2 the second ...body}
There is no need to declare a function. You only need to define before execution.
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/bashhelp(){cat << HELPxtitlebar -- change the name of an xterm, gnome-terminal or kde konsoleUSAGE: xtitlebar [-h] "string_for_titelbar"OPTIONS: -h help textEXAMPLE: xtitlebar "cvs"HELPexit 0}# in case of error or if -h is given we call the function help:[ -z "$1" ] && help[ "$1" = "-h" ] && help# send the escape sequence to change the xterm titelbar:echo -e "\033]0;$1\007"#
It is a good programming habit to help other users (and themselves) use and understand scripts.
[Edit] command line parameters
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/bash help(){ cat << HELP This is a generic command line parser demo. USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2HELP exit 0} while [ -n "$1" ]; docase "$1" in -h) help;shift 1;; # function help is called -f) opt_f=1;shift 1;; # variable opt_f is set -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2 --) shift;break;; # end of options -*) echo "error: no such option $1. -h for help";exit 1;; *) break;;esacdone echo "opt_f is $opt_f"echo "opt_l is $opt_l"echo "first arg is $1"echo "2nd arg is $2"
You can run the script like this:
cmdparser -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.
[Edit] shell script example
[Edit] general programming steps
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.
[Edit] convert binary to decimal
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/bash # VIM: Set Sw = 4 ts = 4 et: Help () {cat
The script uses decimal and binary weights (, 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.
[Edit] copy objects cyclically
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/bash# vim: set sw=4 ts=4 et: ver="0.1"help(){ cat << HELP rotatefile -- rotate the file name USAGE: rotatefile [-h] filename OPTIONS: -h help text EXAMPLE: rotatefile out This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1[BR] and create an empty out-file The max number is 10 version $verHELP exit 0} error(){ echo "$1" exit 1} while [ -n "$1" ]; do case $1 in -h) help;shift 1;; --) break;; -*) echo "error: no such option $1. -h for help";exit 1;; *) break;; esacdone # input check:if [ -z "$1" ] ; then error "ERROR: you must specify a file, use -h for help"fi filen="$1"# rename any .1 , .2 etc file:for n in 9 8 7 6 5 4 3 2 1; do if [ -f "$filen.$n" ]; then p=`expr $n + 1` echo "mv $filen.$n $filen.$p" mv $filen.$n $filen.$p fidone # rename the original file:if [ -f "$filen" ]; then echo "mv $filen $filen.1" mv $filen $filen.1fi echo touch $filentouch $filen
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)
[Edit] 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
7. 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! :)