1, shell script generally to #! Beginning.
2, printf:printf "%-5s%-10s%-4s\n" No Name Mark. %-5s indicates a string substitution with a left-aligned format of 5 (-left-aligned) and, if not used-to specify alignment, the string is aligned to the right. The width specifies the number of characters reserved for a variable. For name, the reserved width is 10.
3, get the length of the string: var=1213434545345 length=$ (#var) can get the string length, stored in length. Get what kind of Shell:echo $SHELL or echo $. The echo $UID is 0 for super users.
4. Mathematical operations in the shell: You can use Let, (()), and [] to perform basic arithmetic operations, and advanced operations using expr and BC. n1=10 n2=12 Self-added: let n1++ result=$[n1+n2] retult = ' expr 3+4 ' result=$ (expr $n 1 +3)
5, Text descriptor. 0--stdin (Standard input)
1--stdout (Standard output)
2--stderr (standard error)
Use echo $? The exit status can be printed.
Error redirection: ls + 2> out.txt output error information to OUT.txt.
6, the array definition: array_var= (1 2 3 4 5) or array_var[0] = "Test1". echo ${array_var[*]} Outputs all values, echo ${array_var[2]} outputs the second value.
7, obtain the terminal information. Gets the number of rows and columns of the terminal: Tput cols
Tput lines
Cursor movement understand the position (100,100) at: Tput Cup 100 100
Acquisition Time: Date with the following format:
$ date "+%d%B%Y"
2015 February
8, script debugging.
Using option-X, start the trace debug shell script:
$ bash-x script.sh
The-x flag prints out every line of command and the current state of execution. Output to stdout.
You can enable or disable debug printing in a script.
Set-x: Displaying parameters and commands at execution time
Set +x: Disable debugging
SET-V: Display input when the current command reads
Set +v: Suppresses printing of input.
9, functions and parameters.
To define a function:
function fname ()
{
statements;
}
Or
FName ()
{
statements;
}
A function can be called whenever a function name is used:
$ fname;
fname arg1 arg2; passing parameters
FName ()
{
echo $, #访问参数1和参数2
echo "[email protected]"; Print all parameters at once in a list similar to "$*"
}
$ $ is the first parameter
$ A is the second parameter
$n is the nth parameter
"[Email protected]" was expanded to "$" "$" "$" and so on.
10. Use the child shell to generate a separate process
The child shell itself is an independent process. You can use the () operator to define a child shell:
Pwd
(CD/BIN;LS)
Pwd
When executed in a child shell, there is no effect on the current shell, and all changes are limited to the child shell. As in the example above, the two PWD command input is consistent.
11. Read the keyboard command.
Read n characters and deposit variable Var:read-n 2 var
Do not echo the way read password: read-s var
Display message: read-p "Enter input:" var
The string typed in two seconds reads into VAR:READ-T 2 var
12,-GT: greater than;-lt: less than;-ge: greater than or equal to;-le: less than or equal to;-A: logical AND;-O: Logical OR. -ne not equal to,-eq equals.
[$var 1-ne 0-a $var 2-gt 2] var1 is not equal to 0, and VAR2 is greater than 2.
System File Related tests:
[-F $file _var]; Returns true if the given variable contains a normal file path or file name
[-X $var]: If the variable contains a file that is executable, true
[-D $var]: Contains the directory is true.
[-E $var]: Contains the file exists, is true
[-C $var]: Contains character device, true
[-B $var]: Block device, True
[-W $var]: writable, true;-R readable true;-L symbolic connection, true.
string comparison:
[[$str 1 = $str 2]]: Equal returns TRUE.! = does not return true. > is greater than return true, < is less than return true.
[[-Z $STR 1]]: empty string returns True,-n non-null returns TRUE.
Shell scripting Knowledge (i) basic knowledge preparation