Home bash Bash if Command Not Found

Bash if Command Not Found

Author

Date

Category

I write a small script on Bash’e and can not understand why it gives an error

#! / bin / bash
if [["$ 1" -eq '']]
Then.
  Echo 'Enter Your Value (Max = 4882)'
  Read Parametyr.
  Echo $ Parametyr & GT; ...
ELSE.
  Echo $ 1 & gt; ...
fi

If you run the script without a parameter, then everything is fine. And if with the parameter, then in the terminal displays

/ usr / local / bin /_Script name: Line 3: [[4000: Command NOT Found

(4000 is a parameter)


Answer 1, Authority 100%

maybe it’s better not “-eq”, and
“-z” – the string “empty”, i.e. has zero length, or
“-N” – the string is not “empty.”

#! / bin / bash
if [-z "$ 1"]
Then.
  Echo 'Enter Your Value (Max = 4882)'
  Read Parametyr.
  Echo $ Parametyr & GT; ...
ELSE.
  Echo $ 1 & gt; ...
fi

Answer 2, Authority 50%

Add spaces after [[and to ]] :

if [["$ 1" -eq ']]]

Answer 3

I guess if you run

readlink -f $ (Which SH)

You do not get BASH as the return value, and Dash. You have a correct preamble, but it matters only when the script is started as ./test.shh after its execution.

Now you are forcibly running the script through an SH interpreter, which is probably DASH, and the design [[]] is specific for Bash.

That’s why? If you simply replace dual brackets with single (and change #! / Bin / bash on #! / BIN / SH, since your script now uses only POSIX functions), it must be performed by destination.

Demonstration on Debian, Test.sh with content:

#! / bin / bash
if [["String" == "String"]]; Then.
  Echo This Is Bash
fi

This happens:

$ readlink -f $ (Which SH)
/ bin / dash
$ sh test.sh.
Test.sh: 2: Test.sh: [[: Not Found
$ Bash Test.sh.
This Is Bash.
$ CHMOD 755 Test.sh
$ ./test.sh.
This Is Bash.

​​source

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions