Error [: -lt: unary operator expected in shell script Linux [on hold]












1















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









share|improve this question









New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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 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
















1















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









share|improve this question









New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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 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














1












1








1








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









share|improve this question









New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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






share|improve this question









New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Jan 14 at 1:49









Jeff Schaller

39.4k1054125




39.4k1054125






New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Jan 14 at 1:46









Shi Jie TioShi Jie Tio

1273




1273




New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




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 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














  • 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








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










1 Answer
1






active

oldest

votes


















9














Try changing all the +1s after the exprs 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))





share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    Try changing all the +1s after the exprs 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))





    share|improve this answer






























      9














      Try changing all the +1s after the exprs 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))





      share|improve this answer




























        9












        9








        9







        Try changing all the +1s after the exprs 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))





        share|improve this answer















        Try changing all the +1s after the exprs 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))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 14 at 1:58

























        answered Jan 14 at 1:52









        agcagc

        4,60811036




        4,60811036















            Popular posts from this blog

            1300-talet

            1300-talet

            Display a custom attribute below product name in the front-end Magento 1.9.3.8