Chinaunix

标题: shell命令结果重定向到文件,如果文件只读导致... [打印本页]

作者: 螃蟹009    时间: 2016-01-05 10:49
标题: shell命令结果重定向到文件,如果文件只读导致...
我们经常在脚本中这么使用

例如:

SHLIST=`ls 2>>/var/log/test`
SHLIST=`ls 2>>/var/log`

但是假如/var/log/test是个只读文件或者log是个目录等等吧,各种以外情况,

会导致,提示说xxx文件有问题

echo $SHLIST输出为空

请问如何规避这个问题,就是说:
无论重定向的文件是好是坏,不影响我前面命令的执行

作者: jason680    时间: 2016-01-05 10:58
回复 1# 螃蟹009

SHLIST=`ls 2>>/dev/null`
   
作者: 螃蟹009    时间: 2016-01-05 11:00
回复 2# jason680


    某些特殊情况文件有问题(有问题时候希望他不影响前面程序的执行),但是没有问题时候又希望可以正常记录
作者: jason680    时间: 2016-01-05 11:07
回复 3# 螃蟹009

check file before using
   
$ man bash
...

       When  used with [[, the < and > operators sort lexicographically using
       the current locale.  The test command sorts using ASCII ordering.

       -a file
              True if file exists.
       -b file
              True if file exists and is a block special file.
       -c file
              True if file exists and is a character special file.
       -d file
              True if file exists and is a directory.
       -e file
              True if file exists.
       -f file
              True if file exists and is a regular file.

       -g file
              True if file exists and is set-group-id.
       -h file
              True if file exists and is a symbolic link.
       -k file
              True if file exists and its ``sticky'' bit is set.
       -p file
              True if file exists and is a named pipe (FIFO).
       -r file
              True if file exists and is readable.
       -s file
              True if file exists and has a size greater than zero.
       -t fd  True if file descriptor fd is open and refers to a terminal.
       -u file
              True if file exists and its set-user-id bit is set.
       -w file
              True if file exists and is writable.

       -x file
              True if file exists and is executable.
       -G file
              True if file exists and is owned by the effective group id.
       -L file
              True if file exists and is a symbolic link.
       -N file
              True if file exists and has been modified  since  it  was  last
              read.
       -O file
              True if file exists and is owned by the effective user id.
       -S file
              True if file exists and is a socket.
       ...
作者: sync_1521    时间: 2016-01-05 11:57
试试SHLIST=`ls 2|tee -a /var/log` 不过最好还是像楼上所说的check file before using
作者: 螃蟹009    时间: 2016-01-05 12:42
回复 5# sync_1521

这个就是我想要的,在用之前确实应该做个检查
   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2