第三章 shell输入与输出
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ 命令
echo
#!/bin/bash
#echod
echo -e "This echo's 3 new lines\n\n\n"
echo "OK"
echo
echo "This echo's 3 new lines\n\n\n"
echo "The log files have all been done">mylogfile.txt
read
echo -n "First Name:"
read firstname
echo -n "Last Name:"
read lastname subname
echo -e "Your First Name is :${firstname}\n"
echo -e "Your Last Name is :${lastname}\n"
echo -e "Your Sub Name is :${subname}\n"
cat
-v 显示控制符
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
管道 |
grep
tee 即看到输出,又存进文件
who |tee -a who.out ----------------who的结果打印出屏幕并且追加到who.out文件
@标准输入输出和错误
0 1 2
键盘 屏幕 屏幕
@文件重定向
> >> 1> 2>
> nullfile 创建nullfile文件
cat >>term.txt <<CHINAITLAB
> Hello,there i am using a $TERMterminal
> and my username is $LOGNAME
> Bye ...
> CHINAITLAB
重定向标准错误
grep "vvv" vvv 2>> err_message.txt
把命令grep "vvv" vvv命令的错误信息追加到message.txt
标准输出和错误输出的综合使用
cat aaa bbb 1>ccc 2>ddd
把aaa bbb文件的内容输入到ccc文件,错误信息输出到ddd文件。(aaa文件存在,bbb文件不存在)
合并标准输出和标准错误
grep "standard" standard.txt >grep.out 2>&1
抓取standard.txt文件中的standard有关信息,并把错误信息输出到&1(即文件grep.out)
exec commond exec +命令 现有shell关闭,重新开启一个shell
文件描述符
#!/bin/bash
#file_desc
exec 3<&0 0<name.txt
read line1
read line2
exec 0<&3
echo $line1
echo $line2
------这里不会覆盖当前shell
转载于:https://blog.51cto.com/wugai/589961