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
if [["$ 1" -eq ']]]
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
$ 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.