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 "7"] -o [ "$age" -ge " 65" ]
then
echo " You can walk in for free "
elif [ "$age" -gt "7"] -a [ "$age" -lt "65"]
then
echo " You have to pay for ticket "
fi
When I’m trying to open this script it asks me for my age and then it says
./bilet.sh: line 6: [: 7]: integer expression expected
./bilet.sh: line 9: [: missing `]'
I don’t have any idea what I’m doing wrong. If someone could tell me how to fix it I would be thankful, sorry for my poor English I hope you guys can understand me.
chepner
492k70 gold badges517 silver badges674 bronze badges
asked Oct 21, 2013 at 21:36
3
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
answered Oct 21, 2013 at 21:43
kamituelkamituel
34.3k6 gold badges81 silver badges98 bronze badges
2
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.
answered Oct 21, 2013 at 21:45
chepnerchepner
492k70 gold badges517 silver badges674 bronze badges
1
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 "1234n"
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 "1234n"
a='1234
'
# Remove all new line, carriage return, tab characters
# from the string, to allow integer comparison
a="${a//[$'trn ']}"
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/
answered Feb 9, 2018 at 11:56
Mr-IDEMr-IDE
6,8491 gold badge53 silver badges59 bronze badges
1
./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 " "
Dan Lowe
50.6k20 gold badges119 silver badges111 bronze badges
answered May 11, 2017 at 14:28
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
Juan Serrats
1,3585 gold badges24 silver badges30 bronze badges
answered Jul 10, 2017 at 8:24
Harry1992Harry1992
4531 gold badge5 silver badges12 bronze badges
1
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" ]
answered Apr 27, 2020 at 16:37
����� 31. ������ ���������������� ������
� |
Turandot: Gli enigmi sono tre, la morte Caleph: No, no! Gli enigmi sono tre, una la |
� | Puccini |
������������� ����������������� ���� � ��������� �������� �
�������� ���� ����������.
case=value0 # ����� ������� ��������. 23skidoo=value1 # ���� �����. # ����� ����������, ������������ � ����, ��������������� ��������� ���������. # ���� ��� ���������� ���������� � ������� �������������: _23skidoo=value1, �� ��� �� ��������� �������. # ������... ���� ��� ���������� ������� �� ������������� ������� �������������, �� ��� ������. _=25 echo $_ # $_ -- ��� ���������� ����������. xyz((!*=value2 # �������� ��������� ��������.
������������� ������, � ������ ����������������� ��������, �
������ ����������.
var-1=23 # ������ ����� ������ ����������� 'var_1'.
������������� ���������� ���� ��� ���������� � �������. ���
������ �������� ������� ��� ���������.
do_something () { echo "��� ������� ������ ���-������ ������� � "$1"." } do_something=do_something do_something do_something # ��� ��� ����� �������� ���������, �� ������� �� ���������.
������������� ������ ��������. � ������� �� ������
������ ����������������, Bash ������ ����������� �� ��������� �
��������.
var1 = 23 # ���������� �������: 'var1=23'. # � ��������������� ������ Bash ����� ���������� "var1" ��� ��� ������� # � ����������� "=" � "23". let c = $a - $b # ���������� �������: 'let c=$a-$b' ��� 'let "c = $a - $b"' if [ $a -le 5] # ���������� �������: if [ $a -le 5 ] # if [ "$a" -le 5 ] ��� �����. # [[ $a -le 5 ]] ���� �����.
��������� �������� ������������� � ���, ���
�������������������� ���������� �������� «����». ��������������������
���������� �������� «������» (null) ��������, � �� ����.
#!/bin/bash echo "uninitialized_var = $uninitialized_var" # uninitialized_var =
����� ������������ ������ ��������� ��������� = � -eq. ���������, �������� = ������������ ��� ���������
��������� ����������, � -eq — ��� ��������� �����
�����.
if [ "$a" = 273 ] # ��� �� ���������? $a -- ��� ����� ����� ��� ������? if [ "$a" -eq 273 ] # ���� $a -- ����� �����. # ������, ������ ���� ������ ����� ���� �� ���������. # ������... a=273.0 # �� ����� �����. if [ "$a" = 273 ] then echo "�����." else echo "�� �����." fi # �� �����. # ���� ����� � ��� a=" 273" � a="0273". # �������� �������� ��������� ��� ������������� "-eq" �� ���������� ����������. if [ "$a" -eq 273.0 ] then echo "a = $a' fi # ���������� �������� ����������� �� ������. # test.sh: [: 273.0: integer expression expected
������ ��� ��������� ����� ����� � ��������� ��������.
#!/bin/bash # bad-op.sh number=1 while [ "$number" < 5 ] # �������! ������ ���� while [ "number" -lt 5 ] do echo -n "$number " let "number += 1" done # ���� �������� ���������� ��������� �� ������: # bad-op.sh: 5: No such file or directory
������, � ��������� ��������, � �������������� ����������
������ ([ ]), ���������� ���������� ����� � ������� �������. ��.
������ 7-6, ������ 16-4 � ������ 9-6.
������ �������� �� � ��������� ��������� ������� ��-��
�������� ���� �������. ���� ������������ �� ������ ���������
������� �� ��������� ������, �� ��� ������� �� ������ ����
�������� � �� ��������. ���������� �������� �������� �������,
�������� ��� �������� ���������� ��� suid.
������������� ������� — � �������� ��������� ���������������
(������� �� �� ��������) ����� ��������� � �����������
�����������.
command1 2> - | command2 # ������� �������� ��������� �� ������� ������� command1 ����� ��������... # ...�� ����� ��������. command1 2>& - | command2 # ��� �� ������������. ������� S.C.
������������� �������������� ������������ Bash ������ 2 ��� ����, �����
�������� � ���������� ���������� ��������, ����������� ���
����������� Bash ������ 1.XX.
#!/bin/bash minimum_version=2 # ��������� Chet Ramey ��������� ��������� Bash, # ��� ����� ������������� ������� ������ ���������� ���������� ������ $minimum_version=2.XX. E_BAD_VERSION=80 if [ "$BASH_VERSION" < "$minimum_version" ] then echo "���� �������� ������ ����������� ��� ����������� Bash, ������ $minimum ��� ����." echo "������������ ������������� ����������." exit $E_BAD_VERSION fi ...
������������� ������������� ������������ Bash ����� ���������
� ���������� ���������� �������� � Bourne shell (#!/bin/sh). ��� �������,
� Linux �������������, sh �������� ����������� bash, �� ��� �� ������ ����� ���
UNIX-������ ������.
��������, � ������� ������ ���������� ���� �� ����� � �����
MS-DOS (rn), ����� �����������
��������, ��������� ���������� #!/bin/bashrn
��������� ������������. ��������� ��� ������ ����� �������
��������� ������� r �� ��������.
#!/bin/bash echo "������" unix2dos $0 # �������� ��������� ������� �������� ������ � ������ DOS. chmod 755 $0 # �������������� ���� �� ������. # ������� 'unix2dos' ������ ����� �� ������ �� ��������� �����. ./$0 # ������� ��������� ���� ������. # �� ��� �� ��������� ��-�� ����, ��� ������ ������ ���������� # ���� �� ����� � ����� DOS. echo "�����" exit 0
��������, ������������ � #!/bin/sh, �� �����
�������� � ������ ������ ������������� � Bash. ��������� ��
������������� �������, �������� Bash, ����� ���������
������������ � �������������. ��������, ������� ������� �������
������� �� ���� �����������, ��������� � Bash, ������ ����������
������� #!/bin/bash.
�������� �� ����� �������������� ���������� ������������� �������� — ��������.
����� ��� � �������, ������� ����� ������������ ����� ��������,
�� �� ���������.
WHATEVER=/home/bozo export WHATEVER exit 0
bash$ echo $WHATEVER bash$
������ ������� — ��� ������ � ��������� ������ ����������
$WHATEVER ��������� ��������������������.
������������� � ����������� ���������� � ���� �� �������, ���
� � ������������ �������� ����� �� ������ ����������
����������.
������ 31-1. ������� � �����������
#!/bin/bash # ������� � �����������. outer_variable=�������_���������� echo echo "outer_variable = $outer_variable" echo ( # ������ � ����������� echo "������ ����������� outer_variable = $outer_variable" inner_variable=����������_���������� # ���������������� echo "������ ����������� inner_variable = $inner_variable" outer_variable=����������_���������� # ��� �������? ������� ������� ����������? echo "������ ����������� outer_variable = $outer_variable" # ����� �� ����������� ) echo echo "�� ��������� ����������� inner_variable = $inner_variable" # ������ �� ���������. echo "�� ��������� ����������� outer_variable = $outer_variable" # �������_����������. echo exit 0
�������� ������ �� echo �� ��������� ������� read ����� ������ �����������
����������. � ���� ��������, ������� read ��������� ���, ��� ����� �� ���
���� �������� � �����������. ������ ��� ����� ������������
������� set (��. ������ 11-14).
������ 31-2. �������� ������ �� ������� echo �������
read, �� ���������
#!/bin/bash # badread.sh: # ������� ������������� 'echo' � 'read' #+ ��� ������ �������� � ����������. a=aaa b=bbb c=ccc echo "���� ��� ���" | read a b c # ������� �������� �������� � ���������� a, b � c. echo echo "a = $a" # a = aaa echo "b = $b" # b = bbb echo "c = $c" # c = ccc # ������������ �� ���������. # ------------------------------ # �������������� �������. var=`echo "���� ��� ���"` set -- $var a=$1; b=$2; c=$3 echo "-------" echo "a = $a" # a = ���� echo "b = $b" # b = ��� echo "c = $c" # c = ��� # �� ���� ��� ��� � �������. # ------------------------------ # �������� ��������: � ����������� 'read', ��� ������� ��������, ���������� ������������� ���������. # �� ������ � �����������. a=aaa # ��� �������. b=bbb c=ccc echo; echo echo "���� ��� ���" | ( read a b c; echo "������ �����������: "; echo "a = $a"; echo "b = $b"; echo "c = $c" ) # a = ���� # b = ��� # c = ��� echo "-------" echo "�������: " echo "a = $a" # a = aaa echo "b = $b" # b = bbb echo "c = $c" # c = ccc echo exit 0
�������� ����, ��� ������������ �������, ������������
������������� � �������� ������, � ������������� ����� «suid». [1]
������������� ��������� � �������� CGI-���������� �����
��������� � ��������� ��������� ��-�� ���������� �������� �����
����������. ����� ����, ��� ����� ����� ���� �������� ����������
�� ��� ����������� ��������.
Bash �� ������ ��������� ������������ ������, ���������� ������� ���� (//).
�������� �� ����� Bash, ��������� ��� Linux ��� BSD ������,
����� ����������� ���������, ����� ��� ��� ��� ������ ����
�������� � ������������ ������ UNIX. ����� ��������, ��� �������,
���������� GNU-������ ������ � ������, ������� ����� ������
����������������, ������ �� ������� � UNIX. ��� ��������
����������� ��� ����� ������ ��������� ������, ��� tr.
� |
Danger is near thee — Beware, beware, beware, beware. Many brave hearts are asleep in the deep. So beware — Beware. |
� | A.J. Lamb and H.W. Petrie |
By the look of things, your result
variable has a .
in it after the number making bash not recognise it as such. You can reproduce the error by simply doing:
[ 7. -gt 1 ]
If you add more of the script to your question, |I can suggest where this might be coming from.
Update
Looking at the full script, I would just replace the line:
result=$(echo "$used / $total * 100" |bc -l|cut -c -2)
With:
result=$(( 100 * used / total ))
Since used
and total
are integers and bash
does integer arithmetic, though note the shifting of the multiplication be 100 to the beginning. Or if you want to ensure correct rounding (‘integer division’ in computing always effectively rounds down):
result=$( printf '%.0f' $(echo "$used / $total * 100" | bc -l) )
This will ensure that there are no trailing dots in result
. The approach using cut
is not a very good idea since it is only valid for result in the range 10-99. It will fail for a result
from 0-9 (as in your case) and also numbers above 99.
Update 2
From @Stephane’s comment below, you are better to round down when comparing to thresholds. Considering this, there is another small error with the snippet in the question — notice the inconsistency between the comparisons used for the warn_level
and the critical_level
. The comparisons for warn_level
are correct, but critical_level
uses -le
(lesser or equal) instead of -lt
(just lesser). Consider when result
is slightly larger than critical_level
— it will be rounded down to critical_level
and not trigger the critical warning even though it should (and would if a -lt
comparison was used).
Perhaps not much of an issue, but here is the corrected code:
if [ "$result" -lt "$warn_level" ]; then
echo "Memory OK. $result% used."
exit 0;
elif [ "$result" -lt "$critical_level" ]; then
echo "Memory WARNING. $result% used."
exit 1;
else
echo "Memory CRITICAL. $result% used."
exit 2;
fi
The -ge
tests are also redundant since these cases are implied on reaching the elif
/else
, so have been removed.
The Bash script is the most frequently used programming language for dealing with the Linux environment. It supports all the basics of datatypes of variables to perform operations such as comparing variables and generating results accordingly. However, the user might face the error of “integer expression expected” while writing any script, which stops the script’s working. The reason is typically caused when the wrong bash syntax is utilized such as using the wrong comparison operator.
This post will demonstrate the reasons and solutions for the error “integer expression expected error” in bash.
- Incorrect Use of Comparison Operator
- Solution: Utilize the Correct Comparison Operator
Reason: Incorrect Use of Comparison Operator
The error is invalid bash syntax, such as using the non-integer value instead of an integer while comparing them. In the following script, the two strings are compared to check whether they are equal or not using the “eq” operator, but the comparison operator is wrong here. As the “eq,” “lt,” and “gt” type of operator deals with integer values only so it will display the error that “integer expression expected”:
#!/bin/bash string1="hello" String2="hello" if [ "$string1" -eq "$string2" ] then echo "Strings Are Equal" fi
Save and run the script in the terminal:
The error occurred on the line “integer expression expected.”
Solution: Utilize the Correct Comparison Operator
To fix this error, use the correct Bash syntax and use the integer value where required instead of non-integer values. In our case, using the comparison operator for comparing the two strings will resolve the error.
Operators | Scenarios to use |
---|---|
-eq, -lt, -gt, -ge, -le, -ne | Use these operators when comparing operands, i.e., “integer.” |
=, != | These operators are utilized when comparing operands are “string.” |
So, according to the above table, the “-eq” operator should be replaced with the “=” to compare the string in the script. Let’s apply this in our script and check the results:
#!/bin/bash string1="hello" String2="hello" if [ "$string1"="$string2" ] then echo "Strings Are Equal" fi
Once the script is modified, save and exit the file.
Run the script using the bash command in the terminal:
The error is resolved, and both strings are equal.
Conclusion
The “integer expression expected” error occurs if the wrong bash syntax is utilized, such as non-integer values instead of an integer. To fix this error, use the correct bash syntax for integer and non-integer values such as -eq, -lt, -gt, -ge, -le, and -ne operators require integer operands to compare. While “=” and “!=” operators require both strings operands to compare.
This write-up has illuminated the reason and the solution for the error “integer expression expected” in the bash script.
This Bash script produces multiple «integer expression expected» errors when run:
echo "Enter string"
read str
len=`echo $str|wc -c`
len=`expr $len - 1`
i=0
j=1
l=`expr $len / 2`
while [ $i -le $l ]
do
start=`echo $str|cut -c $j`
end=`echo $str|cut -c $len`
echo $start
echo $end
if [ $start -ne $end ]
then
echo "not palindrome"
exit 0
fi
len=`expr $len - 1`
i=`expr $i + 1`
j=`expr $j + 1`
done
echo "string is palindrome"
This is the output, including errors:
Enter string
saurabh
s
h
main.bash: line 18: [: s: integer expression expected
a
b
main.bash: line 18: [: a: integer expression expected
u
a
main.bash: line 18: [: u: integer expression expected
r
r
main.bash: line 18: [: r: integer expression expected
string is palindrome
What is wrong in the script?
Eliah Kagan
116k54 gold badges313 silver badges488 bronze badges
asked Dec 17, 2019 at 18:25
0
Instead of using external commands like wc
, expr
, cut
, you can and should use bash internal string manipulation commands. Also, your script is suited to use bash arithmetic operations.
So, I have revised your script as shown below. See also my comments in the script.
#!/bin/bash
echo -n "Enter a string > "
read str
#len=`echo $str|wc -c`
#len=`expr $len - 1`
# You could use just `echo -n ...` and skipped the subtraction.
# But, for a better alternative use this:
let len=${#str}
let i=0
let j=1
#l=`expr $len / 2`
let l=len/2
while (( i < l )) ; do
#start=`echo $str|cut -c $j`
start=${str:j-1:1}
#end=`echo $str|cut -c $len`
end=${str:len-1:1}
echo "$start" '=?' "$end"
if [[ "$start" != "$end" ]] ; then
echo "Not a palindrome"
exit 0
fi
#len=`expr $len - 1`
let len=len-1
#i=`expr $i + 1`
let i++
#j=`expr $j + 1`
let j++
done
echo "String is a palindrome"
This script can still be optimized a bit further. This is left as an exercise for you! ☺
Please, note that if you are using non-ASCII characters in your test string, the appropriate locale should be set. For example:
$ LANG=C.UTF-8 ./pal.sh
Enter string > ΝΙΨΟΝΑΝΟΜΗΜΑΤΑΜΗΜΟΝΑΝΟΨΙΝ
Ν =? Ν
Ι =? Ι
Ψ =? Ψ
Ο =? Ο
Ν =? Ν
Α =? Α
Ν =? Ν
Ο =? Ο
Μ =? Μ
Η =? Η
Μ =? Μ
Α =? Α
String is a palindrome
$ LANG=C ./pal.sh
Enter string > ΝΙΨΟΝΑΝΟΜΗΜΑΤΑΜΗΜΟΝΑΝΟΨΙΝ
� =? �
Not a palindrome
answered Dec 19, 2019 at 16:46
FedKadFedKad
8,7665 gold badges37 silver badges74 bronze badges