我试图在两个文件夹上运行diff
命令,检查返回值,然后输出一条消息。
我有这个:
#!/bin/bash
result='diff dir1 dir2'
if result == 0
then
echo "OK"
else
echo "ERROR"
fi
但是我得到result: command not found
我应该如何执行命令并返回要在IF
中进行比较的值?
答案 0 :(得分:4)
这里有些问题。这是一个例子,但是您将来应该做更多的研究。
#!/bin/bash
result="$(diff dir1 dir2)"
ret=$?
if [[ $ret -eq 0 ]]; then
then
echo "OK"
else
echo "ERROR"
fi
exit $ret
答案 1 :(得分:3)
最简单的方法是直接检查它:
#!/bin/bash
if diff dir1 dir2; then
echo "OK"
else
echo "ERROR"
fi
如果您不希望diff
打印任何内容,请使用-q
标志。
答案 2 :(得分:0)
请改用${result}
。
另外,请注意实际命令上的反引号
#!/bin/bash
result=`diff dir1 dir2`
if [[ -z ${result} ]]
then
echo "OK"
else
echo "ERROR"
fi