捕获文本时检查 bash 中的 return 代码
Check return code in bash while capturing text
当 运行 一个 ldapsearch 时,我们得到一个 return 指示成功或失败的代码。这样我们就可以使用 if 语句来检查是否成功。
如果证书验证失败,则使用调试失败时会打印出来。如何在检查 ldapsearch 成功或失败时捕获命令的输出?
ldapIP=`nslookup corpadssl.glb.intel.com | awk '/^Address: / { print }' | cut -d' ' -f2`
server=`nslookup $ldapIP | awk -F"= " '/name/{print }'`
ldap='ldapsearch -x -d8 -H "ldaps://$ldapIP" -b "dc=corp,dc=xxxxx,dc=com" -D "name@am.corp.com" -w "366676" (mailNickname=sdent)"'
while true; do
if [[ $ldap ]] <-- capture text output here ??
then
:
else
echo $server $ldapIP `date` >> fail.txt
fi
sleep 5
done
正如@codeforester 所建议的,您可以使用 $?
检查最后一个命令的 return 代码。
ldapIP=`nslookup corpadssl.glb.intel.com | awk '/^Address: / { print }' | cut -d' ' -f2`
server=`nslookup $ldapIP | awk -F"= " '/name/{print }'`
while true; do
captured=$(ldapsearch -x -d8 -H "ldaps://$ldapIP" -b "dc=corp,dc=xxxxx,dc=com" -D "name@am.corp.com" -w "366676" "(mailNickname=sdent)")
if [ $? -eq 0 ]
then
echo "${captured}"
else
echo "$server $ldapIP `date`" >> fail.txt
fi
sleep 5
done
编辑:根据@rici 的建议(因为我忘了这样做)...ldap 需要在 if.
之前 运行
EDIT2:根据@Charles Duffy 的建议(我们会到达那里),我们不需要将命令存储在变量中。
当 运行 一个 ldapsearch 时,我们得到一个 return 指示成功或失败的代码。这样我们就可以使用 if 语句来检查是否成功。
如果证书验证失败,则使用调试失败时会打印出来。如何在检查 ldapsearch 成功或失败时捕获命令的输出?
ldapIP=`nslookup corpadssl.glb.intel.com | awk '/^Address: / { print }' | cut -d' ' -f2`
server=`nslookup $ldapIP | awk -F"= " '/name/{print }'`
ldap='ldapsearch -x -d8 -H "ldaps://$ldapIP" -b "dc=corp,dc=xxxxx,dc=com" -D "name@am.corp.com" -w "366676" (mailNickname=sdent)"'
while true; do
if [[ $ldap ]] <-- capture text output here ??
then
:
else
echo $server $ldapIP `date` >> fail.txt
fi
sleep 5
done
正如@codeforester 所建议的,您可以使用 $?
检查最后一个命令的 return 代码。
ldapIP=`nslookup corpadssl.glb.intel.com | awk '/^Address: / { print }' | cut -d' ' -f2`
server=`nslookup $ldapIP | awk -F"= " '/name/{print }'`
while true; do
captured=$(ldapsearch -x -d8 -H "ldaps://$ldapIP" -b "dc=corp,dc=xxxxx,dc=com" -D "name@am.corp.com" -w "366676" "(mailNickname=sdent)")
if [ $? -eq 0 ]
then
echo "${captured}"
else
echo "$server $ldapIP `date`" >> fail.txt
fi
sleep 5
done
编辑:根据@rici 的建议(因为我忘了这样做)...ldap 需要在 if.
之前 运行EDIT2:根据@Charles Duffy 的建议(我们会到达那里),我们不需要将命令存储在变量中。