gpt4 book ai didi

linux - 如何从主脚本中的下标获取退出代码 - bash

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:20 26 4
gpt4 key购买 nike

如何从主脚本中的下标获取退出代码。在我下面的脚本中,当下标失败时,它也会从主脚本中退出。

#!/bin/bash
function reportError() {
if [ $1 -ne 0 ]; then
echo $2
exit $1
fi
}

#executing the subscript

/data/utility/testFolder.sh
#calling the function if there is any error then function would update the
#audit table
reportError $? "job is failed, please check the log for details"

下标代码-

#!/bin/bash

if [ -d "/data/myfolder/testfolder" ]
then
echo "ERROR: Directory does not exists"
exit 1
else
echo "INFO: Directory exists"
exit 0
fi

最佳答案

我检查了你的代码,一切都很好,只是你在下标条件中犯了一个错误。

#!/bin/bash
function reportError() {
if [ $1 -ne 0 ]; then
echo $2
exit $1
fi
}

#executing the subscript

/data/utility/testFolder.sh
#calling the function if there is any error then function would update the
#audit table
reportError $? "job is failed, please check the log for details"

子脚本:

#!/bin/bash

if [ ! -d "/data/myfolder/testfolder" ] # add "!". More details: man test
then
echo "ERROR: Directory does not exists"
exit 1
else
echo "INFO: Directory exists"
exit 0
fi

关于linux - 如何从主脚本中的下标获取退出代码 - bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46204211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com