ShellProgramIs a common file containing Unix commands. the permissions of this file should be readable and executable. Like a common executable program, you can either specify a full path name or put it in the path list specified by the PATH environment variable when executing the shell program.
Like a Common Program, a shell program contains three control structures: sequential structure, judgment, and cyclic structure.
The first line of the shell program should include #! /Bin/sh indicates that the file is an executable program and needs to be interpreted by shell.
1.Pass parameters to the shell program
One way to pass parameters to a shell program is to use environment variables. Pay attention to the differences between environment variables and shell variables. How to pass parameters:
#! /Bin/shecho "you are running in XXX. SH "echo" value of color is $ color "# export color = Black #. /xxx. shyou are running in XXX. shvalue of color is black
Another way to pass parameters to shell is to use command line parameters:
In the shell program, you can use the variable $ plus a number to reference the command line parameters:
Running $0 indicates the command itself
$1 represents 1st Parameters
$ (12) indicates 12th parameters.
$ # Number of command line parameters
$ * Indicates all command line parameters.
#! /Bin/shecho "There are $ # arguments" echo "Thay are $ *" echo "the first argument is $1"
$ # And $ * do not contain $0.
You can pass $ *Commands that receive multiple parameters:
#! /Bin/shecho "$0 will install $ # files to bin directory" chmod A + x $ * MV $ * $ home/binecho "install OK !"
2. Shift command
Command Format: Shift [N]
Function: Move the parameter sequence in $ * to N positions on the left and reduce the value of $ # by N. The shift command will not affect $0. Once this command is completed, the removed parameters will be lost. If necessary, save this parameter before shift is removed.
#! /Bin/shorig_args = $ * echo "There are $ # arguments" echo "Thay are $ *" Shift 2 Echo "There are $ # arguments" echo "Thay are $ *"
3. read command
Command Format: Read variable 1 variable 2... function: Read the value from the standard input and assign it to the specified variable. The input values are separated by blank characters and assigned to the corresponding variables. If the number of variables is greater than the value, the extra variables are set to null. If the number of values is greater than the variable, extra values are assigned to the last variable.
The read command reads the entire line at a time.You cannot use one read command to read a part of a row, but use another read command to read the remaining part of the row.
# Read var1 var2abc def Ghi jkl # echo $ var1abc # echo $ var2def Ghi jkl
4. Method for executing shell program
Whether the shell program is executable or not, you can run the shell program in the subshell of the Current Shell by using the./program name.
If you want to execute the shell program in the current shell, you can use the source program name or. program name (note space) for execution.
5. Return Value of the command
You can use $? To obtain the return value of the previous command. In general, 0 indicates that the previous command ends normally, and 1 indicates that the previous command stops when an error occurs.
$ True # always return the 0 command $ echo $? 0 $ ls $ echo $? 0 $ CPCP: missing file argument try 'cp -- help' for more information. $ echo $? 1
6. Test command
Test command to test the specified conditions and render the results through the return value.
Syntax format: Test expression or [expression]
The test command can be used on three types of objects: digital testing, string testing, and file testing.
For Digital Testing, there are:
-Le less than or equal to-GT greater
-Ge is greater than or equal to-EQ.
-Ne is not equal
$ Var = 12 $ [$ var-lt 24] #12 less than 24? $ Echo $? 0
For string testing, there are:
=! =-Z (String Length is 0)-N (string length is not 0)
[$ Var = "ABC"] # Is the variable VAR equal to ABC? [$ Var! = "ABC"] [-z "$ Var"] # Whether the variable VAR length is 0 [-n "234"] # Whether the variable VAR length is not 0
You can test the File status.
[-F/path/filename] test/path/filename whether it is a common file [-D/path/filename] test/path/filename whether it is a directory [-W/path/filename] Test /path/filename writable?
Available! -A-O (non, And, or) parameters for combination testing
7. Exit command
Syntax format: Exit Number
End the current shell program and specify the return value
8. perform arithmetic operations in Shell
You can use $ (arithmetic expression) to perform arithmetic operations.
$ x = 100 $ echo" $ ($ x + 23 )) "123