shell

cat /dev/null: empty a file
echo $?: view the return value

View all shells in the current system: cat /etc/shells

chsh -l

View the currently logged in shell: echo $SHELL
View the history command: history
Use the 70th history command: ! 70
Execute commands starting with a certain letter: ! [letter]
Execute the last executed historical command: ! !
Display the last part of the previous command: echo ! $
View and set aliases: alias
cancel alias: unalias
cancel alias function: \[command]
The location where alias is stored: Check the system in bashrc of the home directory
So the environment variable: env

Predefined variables:
read standard input: read
operator: expr = $(())
take out the result of the command: `` = $()
display the current program process name: $0
display the current program process number: $$
display the positional parameters Number: $#
Display the content of positional parameters: $*
Escape character: \
Full quote: ''
Partial quote: ""

Integer variables:
+, -, *, /, ()
expr or $(()) Operator:
+, -, \*, /, % remainder (modulo)

Wildcard substitution:
matches zero or more characters: *
matches any character: ?
Match any single character in list: [list]
Match any single character except list: [! list]
matches any single character in c1-c2: [c1-c2]
matches one of sring1 or string2 (or more): {string1,string2,...}

Variable substitution:
${a:-character}
If a is empty or not set, replace a with "character", and the value of a remains unchanged.
When a is not assigned a value,
echo ${a:-tom} The result is: tom
The result of echo $a is empty
${a:+character}
If a is set, replace a with "character", and the value of a remains unchanged.
When a=bob, the
result of echo ${a:+tom} is: tom
The result of echo $a is: bob
${a:=character}
If a is empty or not set, then a is set to the value "character"
When a=bob
echo ${a:=tom} The result is: bob
echo $a The result is: bob echo ${a:=tom}
when a is not assigned the result: tom echo $a result is empty ${a:? prompt} If a is empty or not set, "prompt" is printed as standard error out, this can be used to check if the variable is set correctly echo ${a:?a is not assigned when a=bob } result is: bob echo $a result is: bob echo ${a:?a is not assigned when a is not assigned } The result is: bash: a: a is not assigned echo $a result is empty









Match interception:
${a: number to intercept}
intercept "number to intercept" from the beginning, leaving the rest
When a=abcdefg,
the result of echo ${a:3} is: defg
${a: The number to be intercepted: a few to be left}
intercept the "number to be intercepted" from the beginning, leave "a few to be left", if the number to be left is greater than the rest, leave all The result of echo ${a:2:3}
when a=abcdefg is: cde

When a=001234146
shortest header match interception: ${a#keyword}
echo ${a#*1} result is: 234146
echo ${a#0*2} result is: 34146
longest header match interception: ${a##key word}
echo ${a##*1} result is: 46
shortest end match interception: ${a% keyword}
echo ${a%4*} result is: 0012341
longest end match interception: ${a%% key word}
echo ${a%%4*} result is: 00123
replace the first match: echo ${a/old string/new string}
echo ${a/1/q} result is: 00q234146
Replace all matches: echo ${a//old string/new string}
echo ${a//1/q} The result is: 00q234q46
The length of the variable value: echo ${#variable}
echo ${ #a}Results are: 9

Define an array variable:
a[2]="tom"
a[4]="bob"
echo ${a[2]}
shows the definition of all array contents echo ${a[*]} or echo ${a[@] }
Display the length of the array echo ${#a[*]}
declare -a define the array variable
declare -r define the read-only variable
declare -i define the integer variable
declare -x = expor define the environment variable


function:

Cancel echo: stty -echo
show echo: stty echo

Local effective parameter local


test test command:
return 0 if the condition is true, return 1 if the condition is false
Three types of expressions: file test, string comparison, number comparison
Expression 1 -a Expression 2: Satisfy two expressions at the same time, AND operation
expression Formula 1 -o Expression 2: Only one of the expressions can be satisfied, or the operation
string: -n string length is not zero
-z string length is zero
= two strings are equal
! = two strings are not equal
numbers: -eq equals
-ne not equals
-ge greater than or equals
-gt greater than
-le less than or equal to
-lt less than
-ef have the same inode or device
-nt the former is modified earlier than the latter
- ot The modification time of the former is later than that of the latter
file: -f exists and is a regular file
-d exists and is a directory
-h exists and is a symbolic link
-b block device
-c character device
-e file exists
-r Whether it is readable or not
-w writable
-x executable

Judgment statement:
at the beginning of if, at the end of fi, then executes the command, elif executes the following then if the current result is false, executes the following command if the else above does not match (use test or [] after if to judge and define variables)

stdin: 0
stdout: 1
stderr output: 2

At the beginning of case and at the end of esac, if the defined variable is equal to the value defined below, execute the command of the value

Calling an existing function: . Absolute path to the function file

Loop:
for:
1. Shell mode: for [variable] do loop body done
2. C language mode: for [variable] {loop body}
seq algorithm:
seq 1 2 10 The result is 1, 3, 5, 7, 9
seq 2 2 10 The result is 2, 4, 6, 8, 10
print without wrapping printf
print wrapping echo

while:
execute the loop body in do and done when the condition is true

until:
execute the loop body in do and done when the condition is not established

break terminate
continue jump out

select:
interactive shell script


shell debug
sh -x [script name]
this will execute the script and show the values ​​of all variables
sh -n [script name]
don't execute the script just check the syntax pattern, will return all errors syntax
sh -v [script name]
execute the script Before displaying the script content on the screen


basename and dirname command
basename /etc/passwd result is: passwd
dirname /etc/passwd result is: /etc

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324643570&siteId=291194637