nohup, /dev/null 2>&1,输出重定向

http://mblog.sigma.me/2011/08/15/linux-output-redirect.html

Linux shell中有三种输入输出,分别为标准输入,标准输出,错误输出,分别对应0,1,2。我们可以直接通过输出重定向>(或>>,表示追加)将某种输出重定向到其他地方,如设备,文件,比如:

1 ls > ls.log  #标准输出重定向
2 ls 2> ls.log #标准错误重定向
3 ls > /dev/null #重定向到null设备,相当于直接忽略输出

但是,有时候,我们想把标准输出以及错误输出一起重定向某个文件,这是可以通过 2>&1 实现,也可以通过两个同时重定向到某个文件

1 ls >ls.log 2>&1 //标准输出重定向到ls.log,而错误又重定向到标准输出,这两个位置不可换
2 ls 2>>ls.log 1>>ls.log

 

 

http://hi.baidu.com/zhaolegend/blog/item/245ad226e860bdfed7cae2ed.html

linux重定向及nohup不输出的方法
2010-07-25 9:32

 

先说一下linux重定向:
0、1和2分别表示标准输入、标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出。
在一般使用时,默认的是标准输出,既1.当我们需要特殊用途时,可以使用其他标号。例如,将某个程序的错误信息输出到log文件中:./program 2>log。这样标准输出还是在屏幕上,但是错误信息会输出到log文件中。
另外,也可以实现0,1,2之间的重定向。2>&1:将错误信息重定向到标准输出。
Linux下还有一个特殊的文件/dev/null,它就像一个无底洞,所有重定向到它的信息都会消失得无影无踪。这一点非常有用,当我们不需要回显程序的所有信息时,就可以将输出重定向到/dev/null。

如果想要正常输出和错误信息都不显示,则要把标准输出和标准错误都重定向到/dev/null, 例如:

# ls 1>/dev/null 2>/dev/null

还有一种做法是将错误重定向到标准输出,然后再重定向到 /dev/null,例如:

# ls >/dev/null 2>&1

注意:此处的顺序不能更改,否则达不到想要的效果,此时先将标准输出重定向到 /dev/null,然后将标准错误重定向到标准输出,由于标准输出已经重定向到了/dev/null,因此标准错误也会重定向到/dev/null,于是一切静悄悄:-)

 

由于使用nohup时,会自动将输出写入nohup.out文件中,如果文件很大的话,nohup.out就会不停的增大,这是我们不希望看到的,因此,可以利用/dev/null来解决这个问题。

nohup ./program >/dev/null 2>log &

如果错误信息也不想要的话:

nohup ./program >/dev/null 2>&1 &

 

 

http://czmmiao.iteye.com/blog/911372

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

重定向输入、输出示例
$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

http://blog.csdn.net/flowingflying/article/details/5201199

 

本文也即《Learning the bash Shell》3rd Edition的第七章Input/Output and Command-Line Processing之读书笔记之一。我们曾经学习过shell的基本IO重定向操作:>、<和|。基本上能满足95%的情况,但是我们需要知道bash支持的重定向操作。

  • cmd1 |cmd2 : pipe,将cmd1 的标准输出作为cmd2 的标准输入
  • >file :将标准输出重定向到file
  • <file :将file作为标准输入
  • >>file :将标准输出重定向到file,如果file存在,append到文件中,即附加到文件的后面,而不是覆盖文件

    当cat不带参数的时候,表示使用标准输入作为输入,这允许在标准输入中键入相关的内容,下面将alias加入.bashrc作为最后一行 
    $ cat >> .bashrc 
      alias cdmnt='mount -t iso9660 /dev/sbpcd /cdrom' 
      ^D

  • >|file :强制将标准输出重定向到file,即使noclobber设置。当设置环境变量set –o noclobber,将禁止重定向到一个已经存在的文件中,避免文件被覆盖。
  • n >|file :强制将文件描述符n重定向到file,即使noclobber打开
  • <>file :将file作为标准输入和标准输出。通常用于设备文件(/dev下面的文件),底层系统程序员可用之测试设备驱动,其他的很少用。
  • n <>file :将file作为文件描述符n的输入和输出
  • <<label :Here-document; see text 。将shell的标准输入作为命令的输入,直到行中之包含label。这中间的输入成为here-document。下面是一个例子。我们让人使用cat >> file的方式,通过标准输入在文件中附加内容。

    $ cat >> msgfile << . #这里<<.表明以.为结束。因此无需使用^D,而改用. 
      > this is the text of 
      > our message. 
      > .   #这里表示结束。则msgfile中增加了两行this is…和our message. 
    MACHINE="i586" 
    OS="linux-gnu" 
    CC="gcc" 
    cat > $file <
    Machine: $MACHINE 
    OS: $OS 
    Compiler: $CC 
    EOF 
    查看:cat $file,这里给出正常结构 
    Machine: i586 
    OS: linux-gnu 
    Compiler: gcc 
    如果在EOF加上单引号,或者双引号如下 
    cat > $file <<'EOF' 
    则不解析$CC的内容,文件内容如下 
    Machine: $MACHINE 
    OS: $OS 
    Compiler: $CC 
    如果使用<<-,如下,则删除所有行中前面打头的> tab<这样在脚本的书写上会比较适合阅读 ,例如上面的例子可以写为: 
    cat > $file <<-EOF 
        Machine: $MACHINE 
        OS: $OS 
        Compiler: $CC 
        EOF

  • n>file :将文件描述符n重定向到file
  • n :将file作为文件描述符的输入
  • n>>file :将文件描述符n的输出重定向到file,如果file存在,将输出append到文件后面
  • n>& :将标准输出复制到文件描述符n(Duplicate standard output to file descriptor n)
  • n<& :从文件描述符n复制标准输入(Duplicate standard input from file descriptor n)
  • n>&m :文件描述字n将一个copy至文件描述字m(File descriptor n is made to be a copy of the output file descriptor)
  • n<&m :文件描述字n作为文件描述字m中的一个拷贝(File descriptor n is made to be a copy of the input file descriptor)
  • &>file : 将标准输出和标准错误输出定向至文件file
  • <&- : 关闭标准输入
  • >&- : 关闭标准输出
  • n>&- : 关闭文件描述字作为输出(Close the output from file descriptor n)
  • n<&- :关闭文件描述字作输入(Close the input from file descriptor n)
  • n>&word: If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.
  • n<&word : If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
  • n>&digit- : Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
  • n<&digit- : Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.

文件描述符

  文件描述符在bash中比较少用,从0开始用户表示进行的数据流,0表示标准输入,1表示标准输出,2表示标注错误输出,其他从3开始。最为常用的场景是将错误消息输出到某个文件,可以加上2>file 到我们的命令中。

  我们来看下面一个脚本的例子:

command   > logfile 2>&1 &

  >logfile,表示command的标准输出重定向至文件logfile中,2>&1,匹配n>&m,表示文件描述字2(command的标准错误输出)将copy一份采用文件描述字1(即标准输出),由于标准输出已经重定向logfile,这份copy也见将重定向至文件lofgile。我们可以用“abcd > logfile 2>&1 &”来验证这个效果。最后&表示后台运行的方式。这样命令表示在后台运行command,而它的标准输出和错误输出均重定向到logfile文件中。下面可达到类似的效果:

command 2>&1 | tee logfile &

  错误输出同样适用标准输出,通过pipe方式,见他们作为输入执行tee logfile。tee命令将它的标准输入copy至他的标准标准输出以及参数所带的文件中。和上面的命令不一眼这里即会在stdout 和logfile中同时输出。

其他文件描述字的重定向,例如<&n,通常用于从多个文件中读入或者写出。

<&- ,表示强制关闭标准输入 
>&- ,表示强制关闭标准输出 
1> ,等同于> 
0< ,等同于<

 

posted @ 2012-07-26 15:32  陳聽溪  阅读(23779)  评论(0编辑  收藏  举报