免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1880 | 回复: 5
打印 上一主题 下一主题

[文本处理] shell命令结果重定向到文件,如果文件只读导致... [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-09-19 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-01-05 10:49 |只看该作者 |倒序浏览
我们经常在脚本中这么使用

例如:

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

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

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

echo $SHLIST输出为空

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

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2016-01-05 10:58 |只看该作者
回复 1# 螃蟹009

SHLIST=`ls 2>>/dev/null`
   

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-09-19 06:20:00
3 [报告]
发表于 2016-01-05 11:00 |只看该作者
回复 2# jason680


    某些特殊情况文件有问题(有问题时候希望他不影响前面程序的执行),但是没有问题时候又希望可以正常记录

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
4 [报告]
发表于 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.
       ...

论坛徽章:
12
IT运维版块每日发帖之星
日期:2015-11-17 06:20:00程序设计版块每日发帖之星
日期:2016-01-19 06:20:0015-16赛季CBA联赛之江苏
日期:2016-01-17 15:31:3915-16赛季CBA联赛之上海
日期:2016-01-16 15:44:3015-16赛季CBA联赛之浙江
日期:2016-01-15 20:38:1815-16赛季CBA联赛之北京
日期:2016-01-09 14:30:15CU十四周年纪念徽章
日期:2016-01-07 12:31:5115-16赛季CBA联赛之四川
日期:2016-01-01 11:49:1515-16赛季CBA联赛之深圳
日期:2015-12-24 14:23:4115-16赛季CBA联赛之山西
日期:2015-12-15 16:22:31技术图书徽章
日期:2015-12-10 17:41:0015-16赛季CBA联赛之北控
日期:2016-02-03 10:03:24
5 [报告]
发表于 2016-01-05 11:57 |只看该作者
试试SHLIST=`ls 2|tee -a /var/log` 不过最好还是像楼上所说的check file before using

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-09-19 06:20:00
6 [报告]
发表于 2016-01-05 12:42 |只看该作者
回复 5# sync_1521

这个就是我想要的,在用之前确实应该做个检查
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP