Bash 命令嵌套条件语句失败,但为什么
Bash Command Nested conditional statements failing, but why
我有一个 bash 脚本,其中 运行 有很多条件语句,但是当我 运行 脚本时,它只会命中以下行:if [ $? -eq 0 ]
touch /tmp/converted.json
tool test --json --all-projects | tee >> /tmp/converted.json
ret=$?
set -o pipefail
#no vulns
if ((ret == 0)); then
echo "Hooray!"
#found vulns
elif ((ret == 1)); then
then
#check single project
if [[ $firstchar == "{" ]]
then
echo "SINGLE PROJECT"
elif [[ $firstchar == "[" ]]
then
echo "MULTIPROJECT"
else
echo "didnt get a single or multiproject"
fi
#error with your command
elif ((ret == 2)); then
then
echo "An error occurred"
#no manifest
elif ((ret == 3)); then
then
echo "Ensure your repo has a proper required manifest file"
else
echo "Something else occurred"
fi
那么,为什么会一直满足if((ret==0))
条件呢?
谢谢
$?
具有执行的 last 命令的退出状态。 elif [ $? -eq 1 ]
的最后一个命令是 [
在 if [ $? -eq 0 ]
执行的。
if [ $? -eq 0 ] # executes `[`
then
: dont care
elif [ $? -eq 1 ] # $? here has a nonzero the exit status of `[`
在执行你感兴趣的命令后调用任何命令之前,只需将退出状态保存到变量中。在bash中你可以使用算术展开((...))
。
some_command
ret=$?
if ((ret == 0)); then
: dont care
elif ((ret == 1)); then
我有一个 bash 脚本,其中 运行 有很多条件语句,但是当我 运行 脚本时,它只会命中以下行:if [ $? -eq 0 ]
touch /tmp/converted.json
tool test --json --all-projects | tee >> /tmp/converted.json
ret=$?
set -o pipefail
#no vulns
if ((ret == 0)); then
echo "Hooray!"
#found vulns
elif ((ret == 1)); then
then
#check single project
if [[ $firstchar == "{" ]]
then
echo "SINGLE PROJECT"
elif [[ $firstchar == "[" ]]
then
echo "MULTIPROJECT"
else
echo "didnt get a single or multiproject"
fi
#error with your command
elif ((ret == 2)); then
then
echo "An error occurred"
#no manifest
elif ((ret == 3)); then
then
echo "Ensure your repo has a proper required manifest file"
else
echo "Something else occurred"
fi
那么,为什么会一直满足if((ret==0))
条件呢?
谢谢
$?
具有执行的 last 命令的退出状态。 elif [ $? -eq 1 ]
的最后一个命令是 [
在 if [ $? -eq 0 ]
执行的。
if [ $? -eq 0 ] # executes `[`
then
: dont care
elif [ $? -eq 1 ] # $? here has a nonzero the exit status of `[`
在执行你感兴趣的命令后调用任何命令之前,只需将退出状态保存到变量中。在bash中你可以使用算术展开((...))
。
some_command
ret=$?
if ((ret == 0)); then
: dont care
elif ((ret == 1)); then