Skip to main content

shell

1. Regular Expressions

# Wildcards
Matches any character of any length; this is a wildcard
? Matches any single character; this is a wildcard
# Basic Regular Expressions
\\ Escape character
^ Matches the beginning of a line
$ Matches the end of a line
^$ Represents an empty line
. Matches any single character except newline \n
.* Matches any character of any length
[] Matches any single character contained within [characters]
[^] Matches any single character NOT contained within [^characters]
^[^characters] Matches lines not starting with specified characters
\\{n\\} Matches the preceding item exactly n times, where n is a non-negative integer
\\{n,\\} Matches the preceding item at least n times
\\{n,m\\} Matches the preceding item at least n times and at most m times
# Escape Characters
\\a Bell
\\b Backspace
\\n Newline
\\r Carriage return
\\t Tab character
\\v Vertical tab
\\\\ Represents a single backslash character

2. The 'Three Musketeers' of Scripting

# grep
$ grep options 'KEYWORD' FILE_NAME
Options
-i Ignore case
-v Display lines not matching the pattern, equivalent to ^[^]
-o Display only the matched strings
-n Display matching lines with line numbers
-c Output only the count of matching lines
-l When searching multiple files, output only filenames containing matches
-L Print filenames that do not match
-e Specify multiple pattern conditions
-r Recursive search
-E Use extended regular expressions
-w Display only lines with whole-word matches
-q Suppress output (silent mode)
$ sed options 'line_delimiter /old_string/new_string/ column_delimiter action' FILE_NAME
Delimiter
s Replace specified characters
g Global replacement
i Case-insensitive
Options
-i Edit files in place
-e 'script' -e 'script': Specify multiple editing commands
-n Silent mode (default is print), or display only matched content
Actions:
! Negation, e.g., !W, !=, !d, !i\#, !r \#, !w \#
d Delete
c Replace; c can be followed by a string to replace lines between n1 and n2
p Print; usually used with the -n option
i or i\ : Insert text above the specified line
a or a\ : Append text below the specified line
\n Newline
r FILE: Insert the contents of another file at the specified position
w FILE: Save all matching lines to the specified file
= Display the line number of matching lines
$ awk options 'pattern_or_condition (edit_command)' FILE
# Common built-in variables in awk
FILENAME The name of the file being read by awk
FNR Record number in the current file (cumulative per file)
NF Number of fields in the current record (number of fields in the current line)
$NF Represents the last field
NR Total number of records read (cumulative across all files)

3. Examples

# grep
grep -i 'KEYWORD' FILE_NAME # Case-insensitive filter for KEYWORD
grep -v 'KEYWORD' FILE_NAME # Filter lines not containing KEYWORD
grep -o 'KEYWORD' FILE_NAME # Display only the matched characters
grep -n 'KEYWORD' FILE_NAME # Filter for KEYWORD and print with line numbers
grep -e 'KEYWORD1' -e 'KEYWORD2' FILE_NAME # Filter for KEYWORD1 and KEYWORD2
grep -l 'KEYWORD' FILE_NAME1 FILE_NAME2 # List filenames containing KEYWORD
grep -L 'KEYWORD' FILE_NAME1 FILE_NAME2 # List filenames not containing KEYWORD
grep -w 'KEYWORD' FILE_NAME # Filter as a whole word
grep -q 'KEYWORD' FILE_NAME # Silent mode, no output
grep 'KEYWORD' FILE_NAME # Filter lines containing KEYWORD
grep '^KEYWORD' FILE_NAME # Filter lines starting with KEYWORD
grep 'KEYWORD$' FILE_NAME # Filter lines ending with KEYWORD
grep '^a..b' FILE_NAME # Filter lines starting with a, followed by any two characters, and ending with b
grep 'a*' FILE_NAME # Filter lines containing zero or more occurrences of a
grep -c '^$' FILE_NAME # Count empty lines
# sed
sed 's/aaa/bbb/' FILE_NAME # Replace first occurrence in each line
sed 's/aaa/bbb/g' FILE_NAME # Replace all occurrences in each line
sed 's/aaa//g' FILE_NAME # Replace all occurrences with empty string
sed '2 s/aaa/bbb/' FILE_NAME # Replace first occurrence on line 2
sed '2 s/aaa/bbb/g' FILE_NAME# Replace all occurrences on line 2
sed 's/^/aaa/g' FILE_NAME # Prepend aaa to each line
sed 's/aaa/bbb/2' FILE_NAME # Replace second occurrence in each line
sed '2 s/aaa/bbb/2' FILE_NAME# Replace second occurrence on line 2
sed 'G' FILE_NAME # Add an empty line after each line
sed '1c aaa' FILE_NAME # Replace line 1 with aaa
sed '1,3c aaa' FILE_NAME # Replace lines 1 to 3 with aaa
sed -n '2p' FILE_NAME # Print only line 2
sed -n '1,2p' FILE_NAME # Print lines 1 to 2
sed '2q' FILE_NAME # Print first two lines and quit
sed -n '/ftp/p' FILE_NAME # Print lines containing ftp
sed -n '$p' FILE_NAME # Print the last line
sed -n '2,$p' FILE_NAME # Print lines from 2 to the last line
sed -n '1p;$p' FILE_NAME # Print the first and last lines
sed '2d' FILE_NAME # Delete line 2
sed '$d' FILE_NAME # Delete the last line
sed '1,3d' FILE_NAME # Delete lines 1 to 3
sed '/^\$/d' FILE_NAME # Delete lines starting with $
sed '$!d' FILE_NAME # Delete all lines except the last one
sed '/^$/d' FILE_NAME # Delete empty lines
sed '/aaa$/d' FILE_NAME # Delete lines ending with aaa
# awk
awk 'NR==1{print}' FILE_NAME # Print the first line of the file
awk -F ':' '{print $1}' FILE_NAME # Use : as delimiter, print the first column of the entire file
awk -F':’ '{print $3}' FILE_NAME # Use : as delimiter, print the third column of the file
awk '{print}' FILE_NAME # Use default delimiter (space), print the entire file ($0 represents the whole line)
awk '{print}' < FILE_NAME
cat FILE_NAME |awk '{print}'
awk '{print $0}' FILE_NAME
awk '/root/' FILE_NAME # Match and print lines containing root (pattern matching)
awk -F':' '/^root/' FILE_NAME # Use : as delimiter, print lines starting with root
awk -F':' '/^\<root\>/' FILE_NAME
awk -F':' '/^(root|ftp)/{print $1,$7}' FILE_NAME # Split by colon, print the first and seventh columns of lines starting with 'root' or 'ftp'
awk -F':' '!/^root/' FILE_NAME # Split by colon, print lines not starting with 'root'; '!' denotes negation
awk '/^$/' FILE_NAME # Print empty lines in the file
awk '/2019$/' FILE_NAME # Split by default space delimiter, print lines ending with 2019
awk '/^[Rr]oot/' FILE_NAME # Split by default space delimiter, print lines starting with 'Root' or 'root'
ifconfig ens32|awk '/<inet\>/{print $2}'# Filter network interface IP
# Conditional operation descriptors
Relational operators: >, >=, <, <=, == (exact match), != (not equal)
Assignment operators: =, +=, *=, /=, %=, ^=
# Conditional expression operators
Logical OR ||, Logical AND &&, Logical NOT !
Matching operators: ~ (pattern match, fuzzy match), !~ (no match)
Arithmetic operators: +, -, *, /, %, ^ (exponentiation)

4. Shell Conditional Testing

# Conditional testing
# Test if a specific condition holds; returns 0 if true, otherwise a non-zero value. Determine based on the command's exit status.
0 True Execution successful
Non-zero False Execution failed
# Test command syntax
test condition_expression
# Common conditional tests
File tests
-d Check if it is a directory
-f Check if it is a regular file
-r Check if the current user has read permission
-w Check if the current user has write permission
-x Check if the file has execute permission
-e Check if the directory or file exists
# Examples
test -d /etc/passwd
echo $?
[ -d /etc/ ]
echo $?
[ -f /opt/abc.txt ] || touch /opt/abc.txt
echo $? Returns 0 if the command succeeded, otherwise it failed
# Integer comparison
Given two integers, determine if the first is greater than, less than, or equal to the second
-eq Equal to
-ne Not equal to
-gt Greater than
-lt Less than
-le Less than or equal to; true if either condition is met
-ge Greater than or equal to; true if either condition is met
# String comparison
Check if the user-input string meets requirements
== Indicates the first string is identical to the second string
!= The first string and the second string are different (! means negation)
-z String: Whether the string is empty; false if not empty, true if empty
[-n String]: Whether the string is empty; true if not empty, false if empty
# Examples
abc=www
[ -Z $abc ] && echo "$abc"
[[ -n $www ]lecho $?
a=root
b=ftp
["$a" == "$b" ]:echo $?
["$a"!= "$b" ]:echo $?
a="root"
b="root"
["$a" == "$b" ]:echo $?
# Logical testing of multiple conditions
Test the dependency relationship between two or more conditions
&&/-a Logical AND: Means 'and'; the next step is executed only if both conditions are met
0&& 0=0
0&& 1=1
1 && 0=1
1 && 1=1
# Example
[1 -eq 1] && [2 -gt 1] && echo "Execution correct"
1=1 and 2>1, prints "Execution correct"
[1 -eq 1] && [2 -lt 1] && echo "Execution correct"
1=1 and 2 is not less than 1, does not print "Execution correct"
[1 -eq 1 -a 2 -ne 1] && echo "Execution correct"
||/-o Logical OR: Means 'or'; the next step is executed if at least one condition is met
0||0=0
0||0=0
1||0=0
1||1=1
[1 -eq 1] || [2 -lt 1] && echo "Execution correct"
[1 -eq 1 -o 2 -lt 1] && echo "Execution correct"
1=1 or 2<1, prints "Execution correct"
# ! Logical NOT (negation), ! can be placed inside or outside [ ]
Means negation; if the premise condition is met, the next step is not executed; if not met, the next step is executed
[1 -eq 1 ];echo $?
![1 -eq 1 ];echo $?
[!1 -eq 1 ];echo $?

5. Shell if Statement Types

# Single branch
if [condition test statement]
then
Command sequence
fi
# Double branch
if [condition test statement]
then
Command sequence 1
else
Command sequence 2
fi
# Multiple branches
if [condition test statement 1]
then
Command sequence 1
elif [condition test statement 2]
then
Command sequence 2
else
Command sequence 3
fi

6. Shell for Statement Types

for VARIABLE_NAME in VALUE_LIST
do
Command sequence (loop body)
done
Example
Print 1 to 10
for i in seq `1 10`
do
echo $i
done
# Batch create users starting with 'test', create 10, initial password set to 123456
for i in test{1..10}
do
useradd $i
echo "123456"lpasswd --stdin $i &> /dev/nul
done
# Batch check the survival status of multiple hosts based on a file
IP= $(cat /root/ip.txt)
for i in $IP
do
ping -c2 -i0.2 -W3 $i &> /dev/null
if[$? -eq 0 ]
then
echo "Host $i is up"
else
echo "Host $i is down"
fi
done
# Check if multiple ports on multiple hosts are open (prerequisite: mutual key trust between hosts)
for in 128 129
do
for p in 222325
do
ssh 192.168.80.$i netstat -anptu|grep -q "$p" && echo "port $p is up"
done
done

7. Shell while Statement Types

while [condition expression]
do
Command sequence
done
# Special condition tests
true True condition always holds, infinite loop, unless forcibly terminated.
while true.
do.
echo”123”。
done。
# Example
Calculate the sum from 1 to 100
a= 1
sum=0
while [ $a -le 100 ]
do
sum=' expr $sum + $a^
a='expr$a+1
done
echo $sum

8. Shell case Statement Types

case variable_value in
pattern1)
command_sequence_1
;;
pattern2)
command_sequence_2
;;
*)
default_command_sequence
;;
esac
# Example
Prompt user to input a character to determine if it is a letter, number, or other character
read -p "Enter a character" key
case $key in
[a-z][A-Z])
echo "Letter"
;;
[0-9])
echo "Number"
;;
*)
echo "Other character"
esac
sleep 1 controls the loop speed of the script (sleep for 1s)
seq start_value step end_value
shell loop control structures
The break statement can terminate the execution of while, for, until, or select structures, exiting the loop body
continue skips the current iteration, returns to the start of the loop, and continues with the next iteration.
exit exits the script; commands outside the loop body will not be executed.