如何使shell脚本循环不停止错误

问题描述:

我有一个主shell脚本,称为子脚本,针对一循环的每次迭代,就像这样:如何使shell脚本循环不停止错误

#!/bin/bash 

while read line 
do 
    if [[ $line != "" ]] 
    then 
     ./sslv2_check.sh $line 
    fi 
done < https-servers 

如果任何这些电话在这种情况下降落(请参阅下面的shell脚本)

message="FAIL! $1 supports SSLv2 on port $port" 

然后主脚本将停止并且不会调用下一批。我如何让它继续?

#!/bin/bash 

# Required Argument $1 = hostname 
# Optional Argument $1 = port number 
if [[ $1 == "" ]] 
then 
    echo Error: I expected a hostname to be passed as an argument but didn\'t find any 
    exit 1 
fi 

if [[ $2 == "" ]] 
then 
    port=443 
else 
    port=$2 
fi 

date=$(date +"%Y-%m-%d") 
datetime=$(date +"%Y-%m-%d-%H-%M") 
errorlogfile=logs/$date.error.log 
logfile=logs/$date.log 
# Testing for SSLv2 
output=$(openssl s_client -connect $1:$port -ssl2 2>&1) 
if [[ $output == *"handshake failure"* ]] 
then 
    message="PASS! SSLv2 not supported by $1 on port $port" 
elif [[ $output == *"104"* ]] 
then 
    message="PASS! SSLv2 is not supported by $1 on port $port" 
elif [[ $output == *"null ssl method passed"* ]] 
then 
    message="ERROR! SSLv2 is not enabled on your local machine" 
    # Log error 
    echo "$datetime -- $message" >> $errorlogfile 
    echo $output >> $errorlogfile 
elif [[ $output == *"110"* ]] 
then 
    message="ERROR! Failed to connect to $1. Make sure you type in the hostname correctly etc." 
    # Log error 
    echo "$datetime -- $message" >> $errorlogfile 
    echo $output >> $errorlogfile 
elif [[ $output == *"BEGIN CERTIFICATE"* ]] 
then 
    message="FAIL! $1 supports SSLv2 on port $port" 
    # Log error 
    echo "$datetime -- $message" >> $errorlogfile 
    echo $output >> $errorlogfile 
else 
    message="ERROR! An unknown error occurred. See $errorlogfile for details" 
    echo "$datetime -- $message" >> $errorlogfile 
    echo $output >> $errorlogfile 
fi 
#stdout the message 
echo $message 
#Log the message 
echo "$datetime -- $message" >> $logfile 

一旦openssl连接,它会在关闭之前等待输入。我不知道为什么,但是这导致主批处理脚本中止。该解决方案如下:

更换

output=$(openssl s_client -connect $1:$port -ssl2 2>&1) 

output=$(echo 'GET HTTP/1.0' | openssl s_client -connect $1:$port -ssl2 2>&1) 

您可以试试这个,如果您的其他脚本失败,echo将始终成功。

if [[ $line != "" ]] 
then 
    ./sslv2_check.sh $line || echo "failed" 
fi 
+0

我只是有灵感的尴尬闪光:OpenSSL的等待交互式输入,如果连接成功。所以这个修复它输出= $(echo'GET HTTP/1.0'| openssl s_client -connect $ 1:$ port -ssl2 2>&1) – David 2011-06-16 08:55:35

+0

噢好吧,我没有通过“停止”得到你的意思是“冻结”。 – 2011-06-16 23:28:38

+0

实际上并没有冻结,它停了下来。就像实际返回到shell。 – David 2011-06-30 03:34:45