Bash comparison between strings - equal but not equal for it

前端 未结 2 1807
遇见更好的自我
遇见更好的自我 2021-01-29 02:15

I just wanted to do a really simple comparison between 2 strings in Bash :

stat=`curl -Is $url | head -n 1` 
echo $stat
if [ \"$stat\" = \"HTTP/1.1 200 OK\" ];th         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 02:42

    If you think there might be trailing characters (which you don't mind) then use [[ pattern matching instead:

    # Try not to use `` anymore, backticks are hard to read
    stat=$(curl -Is $url | head -n 1)  
    
    # When debugging, use some sort of delimiter which shows leading or trailing whitespace     
    echo "<$stat>"                          
    
    # Variables inside [[ don't need to be quoted
    # Adding a trailing * allows optional trailing characters
    if [[ $stat == "HTTP/1.1 200 OK"* ]];then
        echo "$symbol is OK"
        echo $stat
        $(( valide++ ))          # You don't need the "$", just the variable name
    else
        echo "$symbol is 404"
        echo $stat
        $(( err++ ))             # using a $ in (( )) can change the scan sequence
    fi
    

提交回复
热议问题