Bash- if clause doesn't work correctly [duplicate] - bash

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
I want to make a shutdown script and it doesn't work as intended. This is what I wrote.
echo "Wanna shutdown (y/n)?"
read ANSWER
if [ $ANSWER=="y" ]
then
sudo shutdown -P now
else
printf "Something...."
whatever i press it just shuts down. Why?

You need to put spaces around the == operator. Otherwise the test expression is a single word, and all non-empty words test successfully.
In addition, if you wish to be portable, you should use = instead of ==. And it is always wise to double quote variable expansions because [ won't do that for you.
if [ "$ANSWER" = y ]; then
On the other hand, if you are using bash (or ksh or zsh) you could use the more forgiving [[ conditional expression:
if [[ $ANSWER = y ]]; then

Related

bash if statement command not found [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 months ago.
I have the simplest issue, but I can't get this to work, and it's driving me crazy. So I am running a script called execute.sh that has a help function. I swear my syntax is correct below; I have tried just one equal sign and now trying two. I have tried the double brackets and continue to get the error "[-h: command not found" . To execute the script I run the following the command.
sh execute.sh -h
Where -h is the argument I am passing int that forces the bash script to call the help function and then exist. But it keeps erroring on the IF Statement. Not sure what else to do, any suggestions?
arg=$1
if ["$arg" == "-h"]; then
helpFunction
exit1;
fi
In
["$arg" == "-h"]
line, you should separate the [ from " also " from the ] with a space. Because [ is actually a command, so it should have a space after it, and the syntax of [ command arguments require a space before ].
arg=$1
if [ "$arg" == "-h" ]; then
helpFunction
exit 1;
fi

bash while loop gives syntax error saying missing '[' [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 1 year ago.
I'm new to bash scripting and am having trouble trying to get this to work
local attempt=1
local success=false
while [[ "$attempt" -le "$retryAttempt" && "$success" -eq "false"]]; do
if ! [[ some condition here]];
then
echo true
return
fi
done
I'm getting an error on the while condition saying
line 10: [: missing ]
I cannot figure out what is wrong here, suggestions anyone?
Short: insert spaces around braces
Long: I wouldn't consider myself a bash pro (no idea what local is), but in a minimal syntax example (which is actually an infinity loop).
while [ $b -le $a] ; do
b=$a
done
gives me the same error. Changing the first line to
while [ $b -le $a ] ; do
works.
Depending on the complexity of the script, you might want to consider python or perl. In my opinion bash syntax can be a real pain in the a**. Especially when passing arguments with spaces through more than one level.

How to make Bash interpret variable as string for comparison? [duplicate]

This question already has answers here:
How to use double or single brackets, parentheses, curly braces
(9 answers)
Closed 3 years ago.
I have a bash script that stores the output of a file comparison. The variable becomes something like: thing="/path/to/file - differ: byte 2, line 3".
In later lines I want to check that thing is not empty. However, when I try comparing them, it interprets thing as a command and not simply as a string.
My code is somewhat as follows:
#!/bin/bash
thing="/path/to/file - differ: byte 2, line 3"
if ["$thing" != ""]; then
echo
echo "Something went wrong"
else
echo "Everything worked"
fi
Rather than saying thing is not an empty string, I get an error message that says something like
bash: [/path/to/thing - differs: byte2, line 3: No such file or directory.
How can I ensure that a comparison is happening between strings and that thing is not being interpreted as a command?
Try this instead:
#!/bin/bash
thing="/path/to/file - differ: byte 2, line 3"
if [[ -n "$thing" ]]; then
echo "Something went wrong"
else
echo "Everything worked"
fi
Notice the double [[]] pairs. That will make use of Bash's internal test.
Use help test on Bash's command prompt to get more information.
EDIT: As explained by Jonathan in the comments, using any of [ … ] or [[ … ]] or test uses a built-in internal command in Bash. Section CONDITIONAL EXPRESSIONS of man bash (or Conditional Expressions, Conditional Constructs and Bourne Shell Built-ins) explains that. However, while [ … ] and test are logically the same (the only difference is that [ expects its last argument to be ] but test has no analogous expectation), the tests implemented by [[ … ]] are subject to different parsing rules from [ … ] and test, and [[ … ]] implements some extra test capabilities missing from the other.

bash eval in if with piped command [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Compare output rather than command
(3 answers)
Closed 3 years ago.
piped eval in bash for string length comparison
i am trying to check if a certain device with a given id is plugged in and trigger a action based on that
i tried eval / exec
here is what i have so far
#!/bin/bash
KBP='[["lsusb -d 1c11:b04d | wc -c" == "0"]]'
if eval $KBP; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
expected result:
if device is plugged in and string is not 0 it would hop in the false condition
actual result - cant evaluate the piped condition
Guessing fixed expression would look like this:
if [ "$(lsusb -d 1c11:b04d | wc -c)" -eq 0 ]; then
To remember:
Bash is spaces aware. [[ and ]] needs after and behind (well, ; is special here, it separates commands).
To get output of a command use command substitution $( ... )
There is no need for eval here.

Hi everyone! can someone tell me what " if [ $# -ne 1 ]"means in a shell script? [duplicate]

This question already has answers here:
Special variables in Unix shells? [closed]
(4 answers)
Is there a list of 'if' switches anywhere?
(5 answers)
Closed 5 years ago.
I have been trying to figure out the following line of code as well:
if [ ! -e $1 ]
thanks
Lets break it down:
$# is the number of remaining arguments
[ is the test command
-ne is the numeric "not equals" operator.
So if [ $# -ne 1 ] is testing if there is exactly one argument (left).
In your second example:
! means not
-e tests if a file exists
$1 is the first remaining argument
Therefore if [ ! -e $1 ] tests that there is no file or directory whose path is given as the first (remaining) argument.
Note that this may fail if the argument is a pathname containing whitespace or globing meta-characters. Quoting is needed to stop word splitting and globbing potentially mangling the pathname; i.e. if [ ! -e "$1" ]

Resources