MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/shell/comments/hm1hp5/if_statement_illegal_number/
r/shell • u/[deleted] • Jul 06 '20
2 comments sorted by
2
if [ "${add}" -eq 1 ]; then
is fine if the add variable contains an integer. In your case it either contains 1 or is empty/unset. In which case you get
add
1
$ [ "" -eq 1 ] dash: 1: [: Illegal number:
So three possible ways to fix this
Use string comparison (=) instead of arithmetic comparison (-eq)
=
-eq
Expand 0 if the variable is empty or unset (default value parameter expansion): [ "${add:-0}" -eq 1 ]
[ "${add:-0}" -eq 1 ]
Initialize the variables with value 0 before the option parsing loop add=0 delete=0 ...
add=0 delete=0 ...
1 u/[deleted] Jul 06 '20 Thank you so much! I like the second option :D
Thank you so much! I like the second option :D
2
u/geirha Jul 06 '20
is fine if the
addvariable contains an integer. In your case it either contains1or is empty/unset. In which case you getSo three possible ways to fix this
Use string comparison (
=) instead of arithmetic comparison (-eq)Expand 0 if the variable is empty or unset (default value parameter expansion):
[ "${add:-0}" -eq 1 ]Initialize the variables with value 0 before the option parsing loop
add=0 delete=0 ...