拥有整个脚本 运行,记录任何失败,但在 AIX 中直到最后才以失败退出
Have whole script run, log any failures, but don't exit with a failure until the end in AIX
所以这可能有点复杂,但请耐心等待。我正在尝试 运行 一个脚本,其中包含多个未嵌套的 if
语句。 if
语句基本上确定是否存在某些内容,如果不存在,那么我希望它记录错误。我不希望脚本以错误退出,直到结束时它应该显示整个脚本的哪些部分没有成功。如果整个脚本没有错误,我基本上希望它说 "Good to go" 或其他内容。
这是我目前所掌握的一些内容。
#Checks if foo exists as a directory
if [ -d "/foo" ]; then
#foo exists as a directory
echo "foo exists"
else
#foo does not exist
echo "foo does not exist."
exit 1
fi
#If the directory above exists create the user
id -u bar > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "bar user exists"
else
echo "bar user doesn't exist"
exit 1
fi
现在,如果其中一个给出的退出代码为 1,那将退出,但我要做的是 运行 他们两个,并让脚本的底部告诉我哪一部分失败,然后根据是否有任何失败以 0 或 1 退出。我想过这样写
if [ -d /foo && -d /bar ];
then
echo "All's good"
exit 0
elif [ -d /foo ];
then
echo "A is fine!"
elif [ -d /bar ];
echo "B is fine"
elif [ ! -d /foo ];
echo "A is not fine"
elif [ ! -d /bar ];
echo "B is not fine"
fi
问题是我认为这不是很优雅,我不能就这样写 if
语句。我想过使用 trap
语句、嵌套的 if
语句,并计算脚本出现的错误数并将其分配给一个变量。问题是,其中 none 似乎是合适的,因为我必须将整个事情编码为 trap
并且使用 if
语句我遇到了同样的问题我到了这里,变量引导我使用 Linux 具有但 AIX 没有的功能,这就是我正在编写的功能。
为其创建一个函数,return 错误数:
check_conditions() {
errors=0
if [ -d /foo ]; then
echo "A is fine!"
else
echo "A is not fine"
(( errors = errors + 1 ))
fi
if [ -d /bar ]; then
echo "B is fine"
else
echo "B is not fine"
(( errors = errors + 1 ))
fi
if [ ${errors} -eq 0 ]; then
echo "All's good"
fi
return ${errors}
}
# main
check_conditions
nr_errors=$?
if [ ${nr_errors} -eq 0 ]; then
echo "Good to go"
else
echo "O dear, ${nr_errors} errors found"
fi
exit ${nr_errors}
当你有很多 dirs 时,你可以创建另一个函数来处理 dirs 或在循环中检查 dirs for dir in /foo /bar; do
。
运行 带有 bash 或 ksh 的代码。
所以这可能有点复杂,但请耐心等待。我正在尝试 运行 一个脚本,其中包含多个未嵌套的 if
语句。 if
语句基本上确定是否存在某些内容,如果不存在,那么我希望它记录错误。我不希望脚本以错误退出,直到结束时它应该显示整个脚本的哪些部分没有成功。如果整个脚本没有错误,我基本上希望它说 "Good to go" 或其他内容。
这是我目前所掌握的一些内容。
#Checks if foo exists as a directory
if [ -d "/foo" ]; then
#foo exists as a directory
echo "foo exists"
else
#foo does not exist
echo "foo does not exist."
exit 1
fi
#If the directory above exists create the user
id -u bar > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "bar user exists"
else
echo "bar user doesn't exist"
exit 1
fi
现在,如果其中一个给出的退出代码为 1,那将退出,但我要做的是 运行 他们两个,并让脚本的底部告诉我哪一部分失败,然后根据是否有任何失败以 0 或 1 退出。我想过这样写
if [ -d /foo && -d /bar ];
then
echo "All's good"
exit 0
elif [ -d /foo ];
then
echo "A is fine!"
elif [ -d /bar ];
echo "B is fine"
elif [ ! -d /foo ];
echo "A is not fine"
elif [ ! -d /bar ];
echo "B is not fine"
fi
问题是我认为这不是很优雅,我不能就这样写 if
语句。我想过使用 trap
语句、嵌套的 if
语句,并计算脚本出现的错误数并将其分配给一个变量。问题是,其中 none 似乎是合适的,因为我必须将整个事情编码为 trap
并且使用 if
语句我遇到了同样的问题我到了这里,变量引导我使用 Linux 具有但 AIX 没有的功能,这就是我正在编写的功能。
为其创建一个函数,return 错误数:
check_conditions() {
errors=0
if [ -d /foo ]; then
echo "A is fine!"
else
echo "A is not fine"
(( errors = errors + 1 ))
fi
if [ -d /bar ]; then
echo "B is fine"
else
echo "B is not fine"
(( errors = errors + 1 ))
fi
if [ ${errors} -eq 0 ]; then
echo "All's good"
fi
return ${errors}
}
# main
check_conditions
nr_errors=$?
if [ ${nr_errors} -eq 0 ]; then
echo "Good to go"
else
echo "O dear, ${nr_errors} errors found"
fi
exit ${nr_errors}
当你有很多 dirs 时,你可以创建另一个函数来处理 dirs 或在循环中检查 dirs for dir in /foo /bar; do
。
运行 带有 bash 或 ksh 的代码。