How to compare strings in Bash

前端 未结 10 1858
眼角桃花
眼角桃花 2020-11-22 02:48

How do I compare a variable to a string (and do something if they match)?

10条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 03:41

    The following script reads from a file named "testonthis" line by line and then compares each line with a simple string, a string with special characters and a regular expression. If it doesn't match, then the script will print the line, otherwise not.

    Space in Bash is so much important. So the following will work:

    [ "$LINE" != "table_name" ] 
    

    But the following won't:

    ["$LINE" != "table_name"] 
    

    So please use as is:

    cat testonthis | while read LINE
    do
    if [ "$LINE" != "table_name" ] && [ "$LINE" != "--------------------------------" ] && [[ "$LINE" =~ [^[:space:]] ]] && [[ "$LINE" != SQL* ]]; then
    echo $LINE
    fi
    done
    

提交回复
热议问题