Simple programming of the shell under Linux

xiaoxiao2021-03-06  65

Simple programming of the shell under Linux

Transfer from: Dynamic Network Production Guide www.knowsky.com

The shell script writes the foundation from timing backup to the execution of the simple command, and Linux's shell script can perform various functions. Almost all programs can be run with the shell script. In the script, you can even contain some simple conditional selection. The basic format of the shell script is as follows: #! / bin / sh ... your commands here ... Note the files start in #! / bin / sh. This statement tells the operating system to explain the program location of the script. Most systems have / bin / sh catalog because this directory contains standard shell programs for root users. In most systems, you can also specify the / bin / bash directory. Each shell's script is different. Some shells, such as Bash, support more commands than standard shells. In most Linux versions, SH is actually BASH. Run from the script is very simple, very like running the command at the Windows DOS prompt. For example, you can copy the file by the following statement: #! / Bin / sh cp file1 file2 mv file2 file3 echo "completion"> Complete.txt Auto execution command is useful for those who do not need manually intervention, but for general The user is not so useful. To do this, the shell script allows the user to enter the command line parameters during the execution, and then run the command with the input parameters. The input parameters in the script are represented by $ 1 to $ 9. If you have written DOS batch files, you will find that there are similar functions in batch files, just by% 1,% 2, etc., represent input parameters. Here, how to use command line parameters: #! / Bin / sh cp $ 1 $ 2 // Incoming two parameters upper script accept 2 command line parameters, the first is the original file to copy, the second is a copy Target file. The command format of the run script is: ./ myscript file1 file2, MyScript represents the script file name. Command line options can also be passed in this way, such as: #! / Bin / sh cp $ 1 $ 2 $ 3 Type ./copy -r SourceDir Desdir form The command to perform the above script, you can recursively copy $ 2 directory all files to $ 3 directory under. Option $ 1 is -r when the CP command is recursively copy all files. The shell script containing conditional choice is generally competent for the task of the excluding variable. However, when performing some decision tasks, there is a need to include if / Then's condition is judged. Shell script programming supports such operations, including comparison, and determination files. The basic IF condition command option is: -EQ - compares whether the two parameters are equal (for example, if [2-Eq 5]) -NE - compares whether the two parameters are not equal - LT - parameter 1 is less than the parameter 2 -LE - Whether the parameter 1 is less than or equal to the parameter 2 -GT - parameter 1 is greater than the parameter 2 -ge - parameter 1 is greater than or equal to parameter 2 -F - check if a file exists (for example, if [-f "fileename") -d - check The directory has almost all judgments that can be implemented with these comparison operators. The Script is often used to check if it exists before performing a file.

转载请注明原文地址:https://www.9cbs.com/read-106588.html

New Post(0)