【问题标题】:Script printing variable expansion脚本打印变量扩展
【发布时间】:2016-06-24 09:20:45
【问题描述】:

我的脚本产生了一些非常奇怪的输出,我不知道为什么。

这是脚本: (userslist.temp 是一个用户名列表,文件的每一行一个)

printf 'Output Starting:\n\n'
while read line; do
    cronresult=$(sudo crontab -u $line -l)
    printf "$line : $cronresult"
done < userslist.temp

它产生的输出:

Output Starting:

no crontab for thornegarvin
thornegarvin : 
no crontab for testuserone
testuserone : 
no crontab for testusertwo
testusertwo : 
no crontab for testuserthree
testuserthree : 

我的用户没有配置 crontab,但我不知道为什么脚本没有在单独的行上打印变量。

【问题讨论】:

  • 如果你通过2&gt;/dev/null丢弃stderr会发生什么?
  • 您的问题到底是什么?没有配置 crontab,我不明白你会看到什么?
  • $(..) 捕获标准输出,而像这样的错误消息直接写入标准错误信息到屏幕。如果您还想在变量中存储错误,可以使用cronresult=$(sudo crontab -u "$line" -l 2&gt;&amp;1)

标签: bash scripting crontab


【解决方案1】:

所以 cmets 足以解决我的问题: 我捕获的输出是标准错误输出,而不是我想象的标准输出。我将标准错误重定向到 /dev/null 并设置了一个 if 来处理空白标准输出。谢谢!

【讨论】:

    猜你喜欢
    • 2012-03-17
    • 1970-01-01
    • 2019-03-31
    • 2018-08-26
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多