如何在Ruby的if语句中检查bash命令的返回值(true/false)?我希望这样的东西能起作用,
if ("/usr/bin/fs wscell > /dev/null 2>&1")
has_afs = "true"
else
has_afs = "false"
end它会报错,错误含义如下,它将始终返回true。
(irb):5: warning: string literal in condition正确的语法是什么?
更新:
/usr/bin/fs wscell 查找已安装和正在运行的afs。它会抛出像这样的字符串,
This workstation belongs to cell <afs_server_name>如果afs未运行,该命令将退出,状态为1
发布于 2013-04-04 03:02:26
你需要的是反引号而不是双引号。要检查程序输出,请执行以下操作:
has_afs = `/usr/bin/fs wscell > /dev/null 2>&1` == SOMETHING ? 'true' : 'false'你要找的东西被填满了。
发布于 2013-04-04 03:07:09
您可能应该使用system()或反引号和,然后检查命令($?.exitstatus)的退出状态
这里有一个很好的快速提示:http://rubyquicktips.com/post/5862861056/execute-shell-commands)
更新:
system("/usr/bin/fs wscell > /dev/null 2>&1") # Returns false if command failed
has_afs = $?.exitstatus != 1 # Check if afs is runninghttps://stackoverflow.com/questions/15795736
复制相似问题