Shell script Detailed definition
A "program" written using the shell's function, which is a plain text file, can write some shell syntax and commands (including external commands) in it, with regular expressions, pipe commands, and data flow redirection.
Use
Automated management, management systems, intrusion detection, continuous command single, simple data processing, cross-platform use, etc.
Specification
#!/bin/bash
declaring that the syntax within this file is the syntax of bash, the system will load bash's related configuration file to execute the following command
# program:
# This program shows ' Hello World ' in your.
# History:
# 2017/06/25 YC First release
description of the script content and features, version information, etc.
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
set some important environment variables, such as path, LANG
......
the main program content
Exit 0
tells the script the execution result $?, which can be used to customize the error message
Execution is performed directly or bash process execution
Absolute path execution/relative path execution/variable path execution
./sh02.sh
The script executes within the bash of the child process. When the child process is complete, the variables or actions within the child process will end without being passed back to the parent process
Source or. Perform
source sh02.sh | | . sh02.sh
The script executes in the parent process.
Logical operations
total=$ (($FIRSTNU * $secnu))
echo $ ((13% 3))
Judging use
test-e filename
["vbird Tsai" = = "Vbird"] # spaces must
Daquan
Flag of the test representative meaning
1. for the "file type" judgment of a file name, such as TEST-E filename indicates that there is no
-E Does the "file name" exist? Common
-F does the "file name" exist and is a file? Common
-D does the "file name" exist and is directory? Common
-B does the "file name" exist and is a block device device?
-C Does the "file name" exist and is a character device?
-S does the "file name" exist and is a Socket file?
-P Does the "file name" exist and is a FIFO (pipe) file?
-L does this "file name" exist and is a link file?
2. Permission detection for files, such as test-r filename indicates readable no ( with the exception of root privileges)
-R detects if the file name exists and has a "readable" permission?
-W detects if the file name exists and has "writable" permissions?
-X detects if the file name exists and has "executable" permissions?
-U detects if the file name exists and has a "SUID" attribute?
-G detects if the file name exists and has a property of "SGID"?
-K detects if the file name exists and has the attribute "Sticky bit"?
-S detects if the file name exists and is "not a blank file"?
3. Comparison between two files , such as: Test file1-nt file2
-nt (newer than) to determine whether File1 is newer than file2
-ot (older than) to determine whether File1 is older than file2
-ef Judge File1 not file2 is the same file, can be used in judging hard link judgment. The main significance in determining whether two files are pointing to the same inode!
4. for a decision between two integers , such as test n1-eq n2
-eq two values equal (equal)
-ne two values unequal (not equal)
-GT N1 greater than N2 (greater than)
-LT N1 smaller than N2 (less than)
-ge n1 greater than or equal to N2 (greater than or equal)
-le N1 less than or equal to N2 (lesser than or equal)
5. Determining The data of a string
Test-z string to determine whether the strings are 0? True if string is an empty string
Test-n string to determine whether strings are not 0? False if string is an empty string. Note:-n can also be omitted
Test STR1 = STR2 Determines whether the str1 equals str2, and if equal, the callback True
Test STR1! = STR2 Determines if the str1 is not equal to str2, and if equal, the callback false
6. Multi-criteria Determination , for example: Test-r filename-a-x filename
-A (and) two conditions at the same time set up! For example Test-r file-a-x file, the file will return true if it has r not x permission.
-O (or) two conditions any one set up! For example Test-r file-o-x file, the file can return true if it has R or X permissions.
! Inverting state, such as test! -x file, which callbacks true when file does not have X
Default variables for Shell script
$ A script file name executed
$ ... The first parameter is $ $, the second parameter is $ ...
$# How many parameters are followed by the script
[email protected] on behalf of "$", "$", "$" ..., each variable is independent
$* represents "$ $ ..."
Shift variable Offset
Shift
Offset a variable, the original $ change to $1,$3 into a $ ...
Shift 3
Offset by three variables, the original $4 becomes $1,$5 ...
Conditional-Judgment # If ... else ... fi
if [condition 1] && [condition 2]; Then
Action 1
elif [Condition 3]; Then
Action 2
Else
Action 3
Fi
# case ... Esac
Case $ variable name in
"1th variable Content")
Action 1
;;
"2nd variable Content")
Action 2
;;
...
*)
The above situation does not conform to the action n
;;
Esac
# function
# Note: function must be written to the front of the program
# Note: Function can also be used in the. Wait, the parameter is not written in ()
function fname () {
Program Segment
}
Loop # While...do...done
Conditions are established, the execution of the procedure section
while [condition]
Do
Program Segment
Done
# Until...do...done
Conditional, terminate the program section
until [conditions]
Do
Program Segment
Done
# for (;;) Do...done
For ((i=1;i<= $nu; i=i+1))
# for (in) Do...done
1 delimited commands
users=$ (cut-d ': '-f 1/ETC/PASSWD)
For username in $users
2 seq Get continuous values
For Sitenu in $ (SEQ 1 100)
3 ls get directory under File
For file in $ (LS $dir)
Tracking and debugging of shell script
sh [-nvx] script.sh
n Do not execute script, query only the syntax problem, no problem no output
V Output Script contents to screen before executing script
x will be used to display the script content on the screen
Shell script Detailed