Simple shell programming in Linux
Transfer from: dynamic network Production Guide www.knowsky.com
Basics of shell script writing
From scheduled backup to simple command execution, Linux shell scripts can execute various functions. Almost all programs can run with shell scripts. The script can even contain some simple conditions. The basic format of shell scripts is as follows:
#! /Bin/sh
...
Your commands here
...
Pay attention to #! Start with/bin/sh. This statement tells the operating system the location of the script program. Most systems have the/bin/sh directory because it contains the standard shell program of the root user. In most systems, you can specify the/bin/bash directory. The scripts for each shell are different. Some shells, such as Bash, support more commands than the standard shell. In most Linux versions, SH is actually bash. Running commands from a script is very simple, like running commands at a Windows DOS prompt. For example, you can use the following statement to copy an object:
#! /Bin/sh
CP file1 file2
MV file2 file3
Echo "complete"> complete.txt
Automatic Command Execution is useful for tasks that do not require manual intervention, but not for general users. Therefore, the shell script allows you to enter command line parameters during execution, and then run the command using the input parameters. The input parameters in the script are represented by $1 to $9. If you have written a DOS batch file, you will find similar features in the batch file, but it uses % 1, % 2, and so on to represent the input parameters. The following example shows how to use command line parameters:
#! /Bin/sh
CP $1 $2 // input two parameters
The preceding script accepts two command line parameters. The first is the original file to be copied, and the second is the target file to be copied. The command format for running the script is./myscript file1 file2, and myscript represents the script file name. The command line option can also be passed in this way, for example:
#! /Bin/sh
CP $1 $2 $3
Run the command in the form of./copy-r sourcedir destdir to execute the preceding script, and recursively copy all the files in the $2 Directory to the $3 directory. When option $1 is-R, the CP command can recursively copy all files.
Shell script with conditional Selection
Simple shell scripts for tasks without variables are generally competent. However, when executing some decision-making tasks, it is necessary to include the IF/then condition judgment. Shell script programming supports such operations, including comparison operations and determining whether a file exists. The basic if condition command options include:
-Eq-compare whether two parameters are equal (for example, if [2-EQ 5])
-Ne-compare whether two parameters are not equal
-Lt-parameter 1: whether it is smaller than parameter 2
-Le-parameter 1: whether it is less than or equal to parameter 2
-GT-whether parameter 1 is greater than parameter 2
-Whether Ge-parameter 1 is greater than or equal to parameter 2
-F-check whether a file exists (for example, if [-F "FILENAME"])
-D-check whether the directory exists
Almost all judgments can be implemented using these comparison operators. In the script, the common-F Command Option checks whether a file exists before executing it.