声明:本文仅作学习研究使用,多数语句都是为了介绍语法而构造的。
重定向输入、输出示例
$cat #cat把键盘看作标准输入,屏幕看作标准输出。按下ctrl+d结束键盘输入
$cat > sample.txt
$cat /dev/null > /var/log/messages
$cat /etc/profile > /var/log/messages
$cat /etc/profile >> /var/log/messages #在文件/var/log/messages末尾追加/etc/profile 的内容
$cat /etc/profile /home/shell.txt > /var/log/messages
$cat /etc/profile /home/shell.txt 1 > hold1 2 > hold2 #将标准输出定向到hold1中,将标准错误输出定向到hold2中
$exec 1> fd1.out #将以后所有命令的输出都定向到fd1.out
$ln -s ch05.doc ./docs >> /tmp/ln.log 2>/dev/null #将连接文件的信息追加到/tmp/ln.log中,并将错误输出定向到/dev/null中
$rm -rf /tmp/my_tmp_dir > /dev/null 2>&1 #将标准错误输出和标准输出都定向到/dev/null中
$who | tee file.a | wc -l #重定向到管道传递给tee命令后继续将结果传递给wc命令
$cat /etc/profile /home/shell.txt | tr "[a-z]" "[a-z]"
$who | sort
$ls | less
将循环的输出重新排序
#!/bin/bash
#filename:output_sort.sh
#datetime:2010_12_24 15:56
#discription:sort the output number
for i in 7 9 2 4 5 12
do
echo $i
done | sort -n //将变量$i中的数值进行排序
exit 0
输入重定向(利用read读入文件/etc/fstab的前两行)
#filename:twolines_fstab
#datetime:2010_12_24 15:59
#discription:output the two lines of fstab
file=/etc/fstab
{
read line1 //读入第一行
read line2 //读入第二行
} < $file
echo "first line in $file is:\"$line1\"" //输出第一行结果
echo "second line in $file is:\"$line2\"" //输出第二行结果
每5分钟将将登录进入系统的用户列表追加到logfile文件中
#filename:record_loginuser.sh
#datetime:2010_12_24 16:16
#discription:record the username who login system every 5 minutes
while : //无限循环开始
date
who
sleep 300 //睡眠5分钟
done >> logfile //将记录的结果重定向到logfile文件中
原创文章,转载请注明出处、作者
如有错误,欢迎指正
作者:czmmiao 原文地址:http://czmmiao.iteye.com/blog/911372