Error [: -lt: unary operator expected in shell script Linux [on hold]
I get error [: -lt: unary operator expected after run below script, anyone can share ideas?
temp=0
while [ $temp -lt 25 ]
do
sleep 1
echo "running.."
if [ $temp -eq 5 ]
then
top -bc -d 5 -n 1|sed -n '7,8p'|awk '{print $1,$9}'>>out.txt
temp=`expr $temp +1`
break
elif [ $temp -eq 20 ]
then
top -bc -d 5 -n 1|sed -n '7,8p'|awk '{print $1,$9}'>>out.txt
temp=`expr $temp +1`
else
temp=`expr $temp +1`
fi
done
linux shell-script test
New contributor
put on hold as off-topic by Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I get error [: -lt: unary operator expected after run below script, anyone can share ideas?
temp=0
while [ $temp -lt 25 ]
do
sleep 1
echo "running.."
if [ $temp -eq 5 ]
then
top -bc -d 5 -n 1|sed -n '7,8p'|awk '{print $1,$9}'>>out.txt
temp=`expr $temp +1`
break
elif [ $temp -eq 20 ]
then
top -bc -d 5 -n 1|sed -n '7,8p'|awk '{print $1,$9}'>>out.txt
temp=`expr $temp +1`
else
temp=`expr $temp +1`
fi
done
linux shell-script test
New contributor
put on hold as off-topic by Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan
If this question can be reworded to fit the rules in the help center, please edit the question.
1
Do you also get errors fromexpr
?
– Michael Homer
Jan 14 at 1:52
no error shown on expr
– Shi Jie Tio
Jan 14 at 1:55
2
Use More Quotes™
– l0b0
2 days ago
1
Next time please tell us ALL the error messages you get. Not just the last one of a set. You would also have gotexpr: syntax error
.
– roaima
2 days ago
add a comment |
I get error [: -lt: unary operator expected after run below script, anyone can share ideas?
temp=0
while [ $temp -lt 25 ]
do
sleep 1
echo "running.."
if [ $temp -eq 5 ]
then
top -bc -d 5 -n 1|sed -n '7,8p'|awk '{print $1,$9}'>>out.txt
temp=`expr $temp +1`
break
elif [ $temp -eq 20 ]
then
top -bc -d 5 -n 1|sed -n '7,8p'|awk '{print $1,$9}'>>out.txt
temp=`expr $temp +1`
else
temp=`expr $temp +1`
fi
done
linux shell-script test
New contributor
I get error [: -lt: unary operator expected after run below script, anyone can share ideas?
temp=0
while [ $temp -lt 25 ]
do
sleep 1
echo "running.."
if [ $temp -eq 5 ]
then
top -bc -d 5 -n 1|sed -n '7,8p'|awk '{print $1,$9}'>>out.txt
temp=`expr $temp +1`
break
elif [ $temp -eq 20 ]
then
top -bc -d 5 -n 1|sed -n '7,8p'|awk '{print $1,$9}'>>out.txt
temp=`expr $temp +1`
else
temp=`expr $temp +1`
fi
done
linux shell-script test
linux shell-script test
New contributor
New contributor
edited Jan 14 at 1:49
Jeff Schaller
39.4k1054125
39.4k1054125
New contributor
asked Jan 14 at 1:46
Shi Jie TioShi Jie Tio
1273
1273
New contributor
New contributor
put on hold as off-topic by Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan
If this question can be reworded to fit the rules in the help center, please edit the question.
1
Do you also get errors fromexpr
?
– Michael Homer
Jan 14 at 1:52
no error shown on expr
– Shi Jie Tio
Jan 14 at 1:55
2
Use More Quotes™
– l0b0
2 days ago
1
Next time please tell us ALL the error messages you get. Not just the last one of a set. You would also have gotexpr: syntax error
.
– roaima
2 days ago
add a comment |
1
Do you also get errors fromexpr
?
– Michael Homer
Jan 14 at 1:52
no error shown on expr
– Shi Jie Tio
Jan 14 at 1:55
2
Use More Quotes™
– l0b0
2 days ago
1
Next time please tell us ALL the error messages you get. Not just the last one of a set. You would also have gotexpr: syntax error
.
– roaima
2 days ago
1
1
Do you also get errors from
expr
?– Michael Homer
Jan 14 at 1:52
Do you also get errors from
expr
?– Michael Homer
Jan 14 at 1:52
no error shown on expr
– Shi Jie Tio
Jan 14 at 1:55
no error shown on expr
– Shi Jie Tio
Jan 14 at 1:55
2
2
Use More Quotes™
– l0b0
2 days ago
Use More Quotes™
– l0b0
2 days ago
1
1
Next time please tell us ALL the error messages you get. Not just the last one of a set. You would also have got
expr: syntax error
.– roaima
2 days ago
Next time please tell us ALL the error messages you get. Not just the last one of a set. You would also have got
expr: syntax error
.– roaima
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
Try changing all the +1
s after the expr
s to + 1
.
Without the space expr
will return an error and $temp
will be empty. If $temp
is empty, then the -lt
will be comparing a blank space to a number, which is why the error appears. Here's a one liner that reproduces the problem:
t=0;t=`expr $t +1`;[ $t -lt 25 ]
Output (to STDERR):
expr: syntax error
bash: [: -lt: unary operator expected
Better yet, change all code that uses the external util expr
to use the shell's own internal arithmetic expansion. So code like this:
temp=`expr $temp +1`
...should be changed to:
temp=$(($temp + 1))
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try changing all the +1
s after the expr
s to + 1
.
Without the space expr
will return an error and $temp
will be empty. If $temp
is empty, then the -lt
will be comparing a blank space to a number, which is why the error appears. Here's a one liner that reproduces the problem:
t=0;t=`expr $t +1`;[ $t -lt 25 ]
Output (to STDERR):
expr: syntax error
bash: [: -lt: unary operator expected
Better yet, change all code that uses the external util expr
to use the shell's own internal arithmetic expansion. So code like this:
temp=`expr $temp +1`
...should be changed to:
temp=$(($temp + 1))
add a comment |
Try changing all the +1
s after the expr
s to + 1
.
Without the space expr
will return an error and $temp
will be empty. If $temp
is empty, then the -lt
will be comparing a blank space to a number, which is why the error appears. Here's a one liner that reproduces the problem:
t=0;t=`expr $t +1`;[ $t -lt 25 ]
Output (to STDERR):
expr: syntax error
bash: [: -lt: unary operator expected
Better yet, change all code that uses the external util expr
to use the shell's own internal arithmetic expansion. So code like this:
temp=`expr $temp +1`
...should be changed to:
temp=$(($temp + 1))
add a comment |
Try changing all the +1
s after the expr
s to + 1
.
Without the space expr
will return an error and $temp
will be empty. If $temp
is empty, then the -lt
will be comparing a blank space to a number, which is why the error appears. Here's a one liner that reproduces the problem:
t=0;t=`expr $t +1`;[ $t -lt 25 ]
Output (to STDERR):
expr: syntax error
bash: [: -lt: unary operator expected
Better yet, change all code that uses the external util expr
to use the shell's own internal arithmetic expansion. So code like this:
temp=`expr $temp +1`
...should be changed to:
temp=$(($temp + 1))
Try changing all the +1
s after the expr
s to + 1
.
Without the space expr
will return an error and $temp
will be empty. If $temp
is empty, then the -lt
will be comparing a blank space to a number, which is why the error appears. Here's a one liner that reproduces the problem:
t=0;t=`expr $t +1`;[ $t -lt 25 ]
Output (to STDERR):
expr: syntax error
bash: [: -lt: unary operator expected
Better yet, change all code that uses the external util expr
to use the shell's own internal arithmetic expansion. So code like this:
temp=`expr $temp +1`
...should be changed to:
temp=$(($temp + 1))
edited Jan 14 at 1:58
answered Jan 14 at 1:52
agcagc
4,60811036
4,60811036
add a comment |
add a comment |
1
Do you also get errors from
expr
?– Michael Homer
Jan 14 at 1:52
no error shown on expr
– Shi Jie Tio
Jan 14 at 1:55
2
Use More Quotes™
– l0b0
2 days ago
1
Next time please tell us ALL the error messages you get. Not just the last one of a set. You would also have got
expr: syntax error
.– roaima
2 days ago