1. Program running environment input and output
- standard input 0
read a;echo $a
- stdout 1
echo ceshiren.com
- error output
ls not_exist_dir
2. Pipe redirection
- Pipes can be redirected between pipes
- Pipes and files can be redirected
echo 11 > /tmp/1
read var </tmp/1
错误输出:
ls not_exist_dir > /tmp/output
将错误输出2重定向到1里
ls not_exist_dir > /tmp/output 2>&1
3. Pipe connection
4. Pipe connector |
- Pipe connectors
|
can connect the execution of multiple programs - Pipe connections are started as child processes
管道符前后是两个命令,{}里面有两个命令,即命令的集合;
echo hogwarts的输出被重定向到下一个命令;
read line; echo input is $line; 读取一行并输出;
echo hogwarts | { read line; echo input is $line; }
curl https://ceshiren.com/categories.json \
| grep -o '{"id[^}]*}' \
| awk -F, '{print $2,$6}' \
| awk -F '"' '{print $7,$4}' \
| sed 's#:##' \
| sort -nr \
| head -5
5. Context control of pipeline execution
- Use { command; }. Pay attention to the spaces and semicolons between the curly braces and the inner command.
- Use control logic while read combination
- Use $() ``
#这个方式无法获得变量x,echo接着通过read命令度过来,无法获得,因为这两个命令通过管道连接起来,是以子进程方式运行,子进程运行完就立马销毁了,可以使用{}将两个命令当作一个进程来进行处理
echo hello world | read x; echo $x
#如下两个方式可以获得变量x
echo hello world | { read x; echo $x; }
echo hello world | while read x; do echo $x; done
6. Introduction to the Three Musketeers of Linux
-
grep: Find lines that meet conditions based on regular expressions
-
awk: Process the segments based on the located data row
-
sed: Modify data based on the located data row
-
Official documentation:
7. Comparison with SQL
Linux Three Musketeers | SQL Structured Query Language |
---|---|
grep data search positioning | select * from table like ‘%xx’ |
awk data slicing | select field from table |
sed data modification | update table set field=new where field=old |
8. BRE basic regular expressions
- ^ start $ end
[a-z]
[0-9]
Range, if there is a ^ at the beginning, it means that the elements in the range cannot be matched.*
0 or more.
Represents any character
9. ERE extended regular expression
- Extensions based on basic regular expressions (BRE)
?
non-greedy matching+
one or more()
Group{}
scope constraints|
Matches any one of multiple expressions
Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:
This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!