shell之通过if [ $? != 0 ]判断上次程序是否执行成功

1、问题

在shell脚本里面有时候我们需要判断上一个程序有没有执行成功,比如用chomd 777 file命令,我们可以用通过if [ $? != 0 ]判断

$?这里表示上一次运行的结果





2、代码实现

#!/bin/bash


test()
{
	return 2;	
}

test

result=$?

echo "result is:"$result

echo "chenyu"

#这里不能写成if [$? != 0]或者if [$? != 0 ]或者if[ $? != 0]
if [ $? != 0 ]; then
    echo "last exe fail"
	exit 1
else
	echo "last exe success"
fi





扫描二维码关注公众号,回复: 1859837 查看本文章

3、运行结果

result is:2
chenyu
last exe success

猜你喜欢

转载自blog.csdn.net/u011068702/article/details/80787549