`integer expression expected` [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
How to use variables inside single quotes
2 answers
I encountered integer expression expected
error in the following code:
#! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi
if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi
Run it and get error report
$ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.
I checked multiple times but fail to locate the bug,
which seem to line-to-line comform to the book
What's the problem with my code?
shell
marked as duplicate by Kusalananda, Isaac, Jeff Schaller, Community⦠Apr 2 at 23:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
How to use variables inside single quotes
2 answers
I encountered integer expression expected
error in the following code:
#! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi
if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi
Run it and get error report
$ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.
I checked multiple times but fail to locate the bug,
which seem to line-to-line comform to the book
What's the problem with my code?
shell
marked as duplicate by Kusalananda, Isaac, Jeff Schaller, Community⦠Apr 2 at 23:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
â Gilles Quenot
Apr 2 at 15:15
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
How to use variables inside single quotes
2 answers
I encountered integer expression expected
error in the following code:
#! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi
if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi
Run it and get error report
$ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.
I checked multiple times but fail to locate the bug,
which seem to line-to-line comform to the book
What's the problem with my code?
shell
This question already has an answer here:
How to use variables inside single quotes
2 answers
I encountered integer expression expected
error in the following code:
#! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi
if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi
Run it and get error report
$ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.
I checked multiple times but fail to locate the bug,
which seem to line-to-line comform to the book
What's the problem with my code?
This question already has an answer here:
How to use variables inside single quotes
2 answers
shell
asked Apr 2 at 15:09
JawSaw
29410
29410
marked as duplicate by Kusalananda, Isaac, Jeff Schaller, Community⦠Apr 2 at 23:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Kusalananda, Isaac, Jeff Schaller, Community⦠Apr 2 at 23:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
â Gilles Quenot
Apr 2 at 15:15
add a comment |Â
FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
â Gilles Quenot
Apr 2 at 15:15
FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
â Gilles Quenot
Apr 2 at 15:15
FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
â Gilles Quenot
Apr 2 at 15:15
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
'$int'
is a quoting error which you would have easily seen if you had run the script through
bash -vx schript.sh
There is no expansion within single quotes. You need:
if [ "$int" -lt 0 ]
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
'$int'
is a quoting error which you would have easily seen if you had run the script through
bash -vx schript.sh
There is no expansion within single quotes. You need:
if [ "$int" -lt 0 ]
add a comment |Â
up vote
4
down vote
accepted
'$int'
is a quoting error which you would have easily seen if you had run the script through
bash -vx schript.sh
There is no expansion within single quotes. You need:
if [ "$int" -lt 0 ]
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
'$int'
is a quoting error which you would have easily seen if you had run the script through
bash -vx schript.sh
There is no expansion within single quotes. You need:
if [ "$int" -lt 0 ]
'$int'
is a quoting error which you would have easily seen if you had run the script through
bash -vx schript.sh
There is no expansion within single quotes. You need:
if [ "$int" -lt 0 ]
answered Apr 2 at 15:15
Hauke Laging
53.2k1282130
53.2k1282130
add a comment |Â
add a comment |Â
FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
â Gilles Quenot
Apr 2 at 15:15