bash - [: -gt: unary operator expected -


I do not write too many bashs, so I'm a little bit stumped about how to fix it. I need to check that the value returned by a command is greater than x when it runs, I get the [: -gt: unary operator required which is I'm unable to fix

This is my script,

  #! / Bin / sh Error = 0 PHPCPDLevel = 100 # PHPCPD "PHP CopyPaste Test (at least $ {PHPCPDLevel}%" PHPCPD = "phpcpd" if echo "[$ PHPCPD echo. Grep"% "| Cut-D ' -f1` -gt "$ PHPCPDLevel"]] Then $ PHPCPD ERROR = 1 else resonates "... - & gt; Only $ PHPCPD | grep"% "| Cut-D" resonates. "-f1 % "Fix buzz" is over! "Exit $ ERROR   

Update: I think I did it:

 PHP code = "PHPCPDLevel = 25 # PHPCPD resonance" PHP CopyPaste probe (limit maximum $ {PHPCPDLevel}%) "PHPCPD =" phpcpd. "PERCENTAGE = $ ($ PHPCPD | grep"% "| Cut-d '. -f1) if [$ {PERCENTAGE} -gt $ $ PHPCPDLevel}] Then $ PHPCPD ERROR = 1 echo resumes "EF $ EXCEPT"% ERROR exit    

Remember that [ is an order.It is built in your shell, but it is still expecting a specific set of command parameters , And when you get something that you do not understand then give you an error. In fact, you can replace [...] to test ... , if it makes it easy to understand:

For example:

  test-gt 34 ​​  

will return:

  bash: test: - Gt: unary operator required   

Hmmm ... same error message.

When you receive things like this, you should see set -xv and set + Xv around the problem area of ​​your shell script in the Set -xv will execute the shell command to execute, and then show you how the command line looks like glitch I mean interpolated

/ Pre>

That $ {PERCENTAGE} is an empty value if you use [...] instead of [[...]] If you do not get the error then [[...]] is parsed slightly differently from [...] because it is a short command initial Shell interpolation is done after the command is parsed, so if you miss a quotation mark or string contains unexpected characters then it is a little more forgiving. PHPCPDLevel = 25 # PHPCPD "PHP CopyPaste Detection (limit $ {PHPCPDLevel}% is maximum") "Export PS4 =" \ $ LINENO: "#by debug set-xv # Line number is executed, debugging turned on PHPCPD = "phpcpd." PERCENTAGE = $ ($ PHPCPD | grep "%" | cut-d '.' -f1) if [[$ {PERCENTAGE} -gt $ {PHPCPDLevel }] Instead of using [[...]] instead of $ PHPCPD error = 1 echo resumes "only $ PERCENTAGE%" Fi set + xv # Exit exit debugging $ ERROR

Now, you will see that the various commands set are returning the environment variable And, potentially, you can see something that you did not expect.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -