Integer expression expected error in shell script

后端 未结 6 1477
自闭症患者
自闭症患者 2020-12-17 08:36

I\'m a newbie to shell scripts so I have a question. What Im doing wrong in this code?

#!/bin/bash
echo \" Write in your age: \"
read age
if [ \"$age\" -le \         


        
相关标签:
6条回答
  • 2020-12-17 08:58
    ./bilet.sh: line 6: [: 7]: integer expression expected
    

    Be careful with " "

    ./bilet.sh: line 9: [: missing `]'
    

    This is because you need to have space between brackets like:

    if [ "$age" -le 7 ] -o [ "$age" -ge 65 ]
    

    look: added space, and no " "

    0 讨论(0)
  • 2020-12-17 09:01

    You can use this syntax:

    #!/bin/bash
    
    echo " Write in your age: "
    read age
    
    if [[ "$age" -le 7 || "$age" -ge 65 ]] ; then
        echo " You can walk in for free "
    elif [[ "$age" -gt 7 && "$age" -lt 65 ]] ; then
        echo " You have to pay for ticket "
    fi
    
    0 讨论(0)
  • 2020-12-17 09:08

    If you are just comparing numbers, I think there's no need to change syntax, just correct those lines, lines 6 and 9 brackets.

    Line 6 before: if [ "$age" -le "7"] -o [ "$age" -ge " 65" ]

    After: if [ "$age" -le "7" -o "$age" -ge "65" ]

    Line 9 before: elif [ "$age" -gt "7"] -a [ "$age" -lt "65"]

    After: elif [ "$age" -gt "7" -a "$age" -lt "65" ]

    0 讨论(0)
  • 2020-12-17 09:11

    Try this:

    If [ $a -lt 4 ] || [ $a -gt 64 ] ; then \n
         Something something \n
    elif [ $a -gt 4 ] || [ $a -lt 64 ] ; then \n
         Something something \n
    else \n
        Yes it works for me :) \n
    
    0 讨论(0)
  • 2020-12-17 09:12

    If you are using -o (or -a), it needs to be inside the brackets of the test command:

    if [ "$age" -le "7" -o "$age" -ge " 65" ]
    

    However, their use is deprecated, and you should use separate test commands joined by || (or &&) instead:

    if [ "$age" -le "7" ] || [ "$age" -ge " 65" ]
    

    Make sure the closing brackets are preceded with whitespace, as they are technically arguments to [, not simply syntax.

    In bash and some other shells, you can use the superior [[ expression as shown in kamituel's answer. The above will work in any POSIX-compliant shell.

    0 讨论(0)
  • 2020-12-17 09:23

    This error can also happen if the variable you are comparing has hidden characters that are not numbers/digits.

    For example, if you are retrieving an integer from a third-party script, you must ensure that the returned string does not contain hidden characters, like "\n" or "\r".

    For example:

    #!/bin/bash
    
    # Simulate an invalid number string returned
    # from a script, which is "1234\n"
    a='1234
    '
    
    if [ "$a" -gt 1233 ] ; then
        echo "number is bigger"
    else
        echo "number is smaller"
    fi
    

    This will result in a script error : integer expression expected because $a contains a non-digit newline character "\n". You have to remove this character using the instructions here: How to remove carriage return from a string in Bash

    So use something like this:

    #!/bin/bash
    
    # Simulate an invalid number string returned
    # from a script, which is "1234\n"
    a='1234
    '
    
    # Remove all new line, carriage return, tab characters
    # from the string, to allow integer comparison
    a="${a//[$'\t\r\n ']}"
    
    if [ "$a" -gt 1233 ] ; then
        echo "number is bigger"
    else
        echo "number is smaller"
    fi
    

    You can also use set -xv to debug your bash script and reveal these hidden characters. See https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-error-integer-expression-expected-934465/

    0 讨论(0)
提交回复
热议问题