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

#!/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/

10-16 11:28