1, the redirector
program = commands (instructions) + data (variables)
in the program, how the input data? How output?
Data Input: keyboard - the standard input, but is not the only way to enter
--stdin
echo "123456" | --stdin passwd "username" and password user same line, do not enter the interface
while line; do
loop Line $
DONE </ etc / passwd
Output data: Display - standard output, but not the only output
ls / etc> a.txt
fd file identifier 0-9
identifier 012 is defined, the rest can define their own manual
0 --- standard output
1 --- standard input
2 --- erroneous input and output (standard error)
common redirection symbols
1, stdout
> coverage redirection, dangerous (original data will be lost upon redirection)
SET -C closing cover redirection
> | forced redirection
>> append redirection, does not cover
2, the standard input
<replace file contents
tr set1 [str2] <file.txt
tr abc ABC </ etc / passwd> abc /etc/passwd.bak in the / etc / passwd file replacement is ABC, the output to passwd.bak
<< The multiple rows of data simultaneously input
cat >> a.txt << EOF (just a standard of what writing can)
> 1
> 2
> 3
> EOF (when input is complete, quit to write their own definition)
3, the output error
2>
expansion: not to output the contents, only the output state;
LS / etc> / dev / null
IF [-eq $ 0?]; The then
loop
Fi
2 >>
&> & 2 == & >> > 1
2、管道 -tee
|(管道符)
command1 | command2 | command3 | ... ...
前一个命令的止盈结果交给后一个命令来执行
【Linux思想:结合小功能,实现大大功能】
free -m | grep "^mem" | cut -d' ' -f19
free -m | grep "^mem" | awk'{print $3}'
tee
一路输入,两路输出
tee /tmp/tee.out //没有文件会创建,默认如果文件内有内容,会被覆盖;
练习:
1、将/etc/passwd文件中的前5行内容转换为大写后保存至/tmp/passwd.out文件中
head -5 /etc/passwd | tr [a-z] [A-Z] > /tmp/passwd.out
2、将登陆至当前系统上用户信息汇总的后三位信息转换为大写后保存至/tmp/who.out文件中;
who | tail -3 | cut -d' ' -f1 | tr [a-z] [A-Z] > /tmp/who.out
tail 查看文件尾部多少行(默认10行)
-n tail -n 5 /etc/passwd == 简写 tail -5 /etc/passwd
-f 实时查看文件更新内容
tail -f /var/log/message
head 查看文件头部多少行(默认10行)
-n # 前#行,简写-#
取10-20行 head -20 /etc/passwd | tail -10