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












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















closed as off-topic by Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan Jan 14 at 12:05


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
    Jan 14 at 2:12






  • 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
    Jan 14 at 8:47
















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















closed as off-topic by Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan Jan 14 at 12:05


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
    Jan 14 at 2:12






  • 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
    Jan 14 at 8:47














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
















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















share|improve this question













share|improve this question




share|improve this question








edited Jan 14 at 1:49









Jeff Schaller

39.7k1054126




39.7k1054126










asked Jan 14 at 1:46









Shi Jie TioShi Jie Tio

1343




1343




closed as off-topic by Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan Jan 14 at 12:05


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.




closed as off-topic by Michael Homer, Stephen Harris, Networker, Vlastimil, Anthony Geoghegan Jan 14 at 12:05


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
    Jan 14 at 2:12






  • 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
    Jan 14 at 8:47














  • 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
    Jan 14 at 2:12






  • 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
    Jan 14 at 8:47








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
Jan 14 at 2:12





Use More Quotes™

– l0b0
Jan 14 at 2:12




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
Jan 14 at 8:47





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
Jan 14 at 8:47










1 Answer
1






active

oldest

votes


















10














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


Or, in bash, that whole line can be substituted with just:



((temp++))





share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10














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


    Or, in bash, that whole line can be substituted with just:



    ((temp++))





    share|improve this answer






























      10














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


      Or, in bash, that whole line can be substituted with just:



      ((temp++))





      share|improve this answer




























        10












        10








        10







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


        Or, in bash, that whole line can be substituted with just:



        ((temp++))





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


        Or, in bash, that whole line can be substituted with just:



        ((temp++))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered Jan 14 at 1:52









        agcagc

        4,64811137




        4,64811137















            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?