Shell学习笔记---重定向输入、输出(原创)

简介:

声明:本文仅作学习研究使用,多数语句都是为了介绍语法而构造的。

重定向输入、输出示例
$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的前两行) 
#!/bin/bash

#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\""   
 //输出第二行结果 
exit 0


每5分钟将将登录进入系统的用户列表追加到logfile文件中
 
#!/bin/bash
#Filename:record_loginuser.sh
#Datetime:2010_12_24 16:16
#Discription:Record the username who login system every 5 minutes
while :      
 //无限循环开始 
do
date
who
sleep 300       
//睡眠5分钟 
done >> logfile     
//将记录的结果重定向到logfile文件中

 

参考至:http://club.topsage.com/viewthread.PHP?tid=668357&highlight=shell
原创文章,转载请注明出处、作者
如有错误,欢迎指正
邮箱:czmcj@163.com

作者:czmmiao 原文地址:http://czmmiao.iteye.com/blog/911372
相关文章
|
3月前
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
39 0
|
4月前
|
Shell 数据处理
Shell编程中,输入/输出重定向和管道
Shell编程中,输入/输出重定向和管道
21 2
|
6月前
|
Unix Shell Linux
|
3月前
|
数据挖掘 Shell
在Shell中,标准输出重定向
在Shell中,标准输出重定向
23 1
|
2月前
|
Unix Shell Linux
在Unix/Linux Shell中,管道(`|`)和重定向
在Unix/Linux Shell中,管道(`|`)和重定向
23 1
|
3月前
|
Java Shell Linux
Shell编程 学习笔记
Shell编程 学习笔记
69 1
|
3月前
|
Shell
在Shell中,您可以同时重定向标准输出(STDOUT)和错误输出(STDERR)
在Shell中,您可以同时重定向标准输出(STDOUT)和错误输出(STDERR)
76 1
|
3月前
|
Shell 数据处理
shell的重定向
shell的重定向
65 2
|
4月前
|
机器学习/深度学习 Unix Shell
Shell编程基础入门(Bash|变量与输入输出重定向2&1)
Shell编程基础入门(Bash|变量与输入输出重定向2&1)
71 0
|
4月前
|
Unix Shell Linux
Shell 输入/输出重定向
Shell 输入/输出重定向
17 0