shell script basis - Syntax

A variable

[root@T_FOOT-Home2-ZZZ01 ~]# a=hello
[root@T_FOOT-Home2-ZZZ01 ~]# echo $a
hello
[root@T_FOOT-Home2-ZZZ01 ~]# echo ${a}_world
hello_world

In fact, $ a $ {A} and the same function, but in order to prevent splicing string, string variables can not distinguish between the computer and splicing, it is necessary to add {}

Two extraction output of the command

[root@T_FOOT-Home2-ZZZ01 ~]# pwd
/root
[root@T_FOOT-Home2-ZZZ01 ~]# echo $(pwd)
/root

Assignment three conditions ($ {var: - / = / / + string?})

1 $ {var: -string}: means if var is empty, then the output value of the string, if var is not empty, then the output value of var

2 $ {var: = string}: refers to a value if var is empty, then the output string, and the string value assigned to var, var values ​​if not empty, then the output of var

3 $ {var:? String}: means if var is empty, then the error message is output -bash: var: string, if the value of var is not empty, then the output of var

4 $ {var: + string}: and $ {var: -string} Conversely, if var is empty, then print the value of var, var if not empty, then print the value of the string

Four $ ((exp))

[root@T_FOOT-Home2-ZZZ01 ~]# echo $((3+2))
5

Five pattern matching replacement

$ {Var% pattern}, $ {var %% pattern} from the right match

$ {Var # pattern}, $ {var ## pattern} from left to match

$ {Var% pattern}, $ {var # pattern} represents the shortest, the matching to stop

$ {Var %% pattern}, $ {var ## pattern} represents the longest match

Wildcards

*: Zero or more of any character

?: Zero or one character

[...]: matching brackets to indicate character

[...!]: That does not match the characters inside the brackets

[root@T_FOOT-Home2-ZZZ01 ~]# f=a.tar.gz
[root@T_FOOT-Home2-ZZZ01 ~]# echo ${f##*.}
gz
[root@T_FOOT-Home2-ZZZ01 ~]# echo ${f#*.}
tar.gz
[root@T_FOOT-Home2-ZZZ01 ~]# echo ${f%%.*}
a
[root@T_FOOT-Home2-ZZZ01 ~]# echo ${f%.*}
a.tar  

Six to extract the bytes

[root@T_FOOT-Home2-ZZZ01 ~]# echo ${f:0:2}
a.

Get the subscript f is from 0 byte two bytes subsequent

[root@T_FOOT-Home2-ZZZ01 ~]# echo  ${f:1}
.tar.gz

After extracting the subscript f is from 1 byte to all bytes

Seven string replacement

${file/dir/path}: Replace the first dir path

${file//dir/path}: Replace all dir path

${#var}:Calculate the value of the variable ${var}length

Eight output redirection

echo "hello world" >> tempfile hello world to copy tempfile to a file, then print it out

Nine input redirection

wc << flag

Its essence is input as a command to output the content cached

Ten Process Control

if-then statement

if xx1 then

 xx1s

elif xx2 then

 xx2s

else 

 xx3s

be

Eleven case

case ... esac

Twelve for

Thirteen while

Fourteen until

Fifteen break and continue

Sixteen test command

Referring connected  https://blog.csdn.net/aimarrow/article/details/80870180

Guess you like

Origin www.cnblogs.com/freeht/p/12133864.html