Unix: If/then statement is always false - bash

This if/then statement in Unix always puts me in the else statement. I am using Bash.
name="Don"
if [ "$name" == "Don" ]; then
echo "Hi Don!"
else
echo "You are not Don. You are: $name"
fi
This is my first Unix shell script, so I'm sure it's something obvious. I've checked against the style guide and other if/then examples, but don't see anything wrong: http://www.dreamsyssoft.com/unix-shell-scripting/ifelse-tutorial.php.

If you're in a POSIX shell don't use ==. Instead use =. == is specific to Bash.
name="Don"
if [ "$name" = "Don" ]; then
echo "Hi Don!"
else
echo "You are not Don. You are: $name"
fi

I executed your script and it jus tworked as expected.
If this it the full code snipped, did you propably forget to call the bash?
I am asking this because when executing the snipped with "sh", it behaves exectly as you said as this is just partial valid for sh.
So I think you missed this:
#!/bin/bash
name="Don"
if [ "$name" == "Don" ]; then
echo "Hi Don!"
else
echo "You are not Don. You are: $name"
fi

There was only one thing wrong in your original code: the line that reads
echo "Hi Don!"
The shell is trying to interpret the special character !
Try putting this line single quotes example:
echo 'Hi Don!'

Related

If statement goes else every time - bash

I am very new here, so I apologize for any my mistakes, and I am sorry for my lack of knowledge (I'm just beginner).
So here it is, i am doing little script in bash with li and I have if statement, here it is
#!/bin/bash
something=$(whiptail --inputbox "Enter some text" 10 30 3>&1 1>&2 2>&3)
if [ $something ?? 'you' ];
then
echo "$something"
else
echo "nope"
fi
To be specific what i want from it - I enter some word/sentence/whatever to whiptail, and if it contains some of of you string then prints it, but every times it goes else ;_;.Please help.
EDIT now it works, thanks but I need to check if string contains word.
if [[ $string =~ .*My.* ]]
doesn't seem to work
I don't get it at all, losing hope and searching the web i've encontered on
#!/bin/bash
OPTION=$(whiptail –title “Menu Dialog” –menu “Choose your option” 15 60 4 \ “1” “Grilled ham” \ “2” “Swiss Cheese” \ “3” “Charcoal cooked Chicken thighs” \ “4” “Baked potatos” 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ];
then echo “Your chosen option:” $OPTION
else echo “You chose Cancel.”
fi
And I've just pasted this script to check how it works and modify it, it isn't mine script and it supposed to work, but it says “You chose Cancel.”
What you may be looking for are the string comparison operators like == or !=. For example,
if [ "$something" == "you" ]; then
echo "$something"
else
echo "nope"
fi
If $something equals you then echo $something; else echo nope.
Or, as David C.Rankin mentioned in his comment you can check the string variable to prove whether a string is empty or not. For example,
if [ -z "$something"] ;then
String is empty
if [ -n "$something" ]; then
String is non-empty
For more information on this check the TEST manual page.

Shell script: Why can't the if statement make a logical comparison?

I'm new to Unix and Linux in general and failed to make a logical comparison within an if statement.
I'm sure this is a very basic mistake but I just can't find the error.
if (7+3 == 10); then
echo "nice this works"
elif (7+3 == 73); then
echo "too bad string concatenation"
else
echo "I really don't understand shell"
fi
Echo: I really don't understand shell.
I would expect you to see this error message twice: 7+3: command not found -- did you?
Single sets of parentheses run the enclosed commands in a subshell, so you're attempting to execute the command 7+3 with two arguments, == and 10 (or 73)
Arithmetic evaluation occurs within double parentheses
if ((7+3 == 10)); then
echo "nice this works"
elif ((7+3 == 73)); then
echo "to bad string concatenation"
else
echo "I really don't understand shell"
fi
nice this works
See http://mywiki.wooledge.org/ArithmeticExpression
The operator your are using == is used for string comparisons. The right way to do it would be with the -eq (equal) operator:
if [ 10 -eq 10 ]; then
echo "10 = 10"
fi
and you also need to use the right parenthesis for the if [ ] (you can look this up in the bash with 'man test')
The correct syntax would be
if [ $((7+3)) -eq 10 ]; then
echo "nice this works"

Shell Script Syntax Error

At the moment I am working on a blackjack game using shell script. I have most of the script working with functions however the method I am using to find out if the player/computer goes bust doesn't seem to work. Could anyone point me in the right direction. (I am new to shell script.) When running it it will throw syntax errors around the lines that begin elif and sometimes if. It also prints all of the 'echo' outputs in bustConfirm instead of only the one that is true.
Also yes, one of my functions is called bustCheck.
bustConfirm(){
bust='bust'
under='under'
if [ $userBust -eq $bust -a $systemBust -eq $bust ]
then
echo "You both went bust! Be more careful!"
endGameRepeat
elif [ $userBust -eq $bust -a $systemBust -eq $under ]
echo $userName "went bust! Congratulations" $systemName"!"
endGameRepeat
elif [ $userBust -eq $under -a $systemBust -eq $bust ]
then
echo $systemName "went bust! Congratulations" $userName"!"
endGameRepeat
else
echo "Nobody went bust! Well played!"
endGameScores
fi
}
bustCheck(){
if [ "$userScore" -gt 21 ]
then
echo $userName "is bust!"
userBust='bust'
else
userBust='under'
fi
if [ "$systemScore" -gt 21 ]
then
echo $systemName "is bust!"
systemBust='bust'
else
systemBust='under'
fi
bustConfirm
}
The idea is that I wanted to use an && in the bustConfirm function and then an || to get the player is bust or system is bust result if only one of them was bust.
Also just a pointer but in the bustCheck I am seeing userBust and systemBust to contain the words bust or under. I created the variables bust and under for the bustConfirm function.
systemScore, userScore, systemName and userName are set before when the script is running.
Hope I've given enough detail and formatted it properly, first proper post so I apologize if not!
Taking a quick look, I see that the first if statement doesn't have a space after the opening square bracket.
I also recommend you put quotes around your variable names in if statements. This is due to the way shell actually works. The bash shell is extremely intelligent, and before your program has a chance to do anything, it grabs the line, does its magic, and then presents the line to the processor.
For example:
foo=""
if [ $foo = "" ]
then
echo "Foo is blank"
fi
Seems simple enough. However, what happens is that your shell will grab the line, substitute the value of $foo for the string "$foo", and then execute the line. Since $foo is blank, your if statement will become:
if [ = "" ] # That's not right!
then
echo "Foo is blank"
fi
By using quotes, this:
foo=""
if [ "$foo" = "" ]
then
echo "Foo is blank"
fi
becomes:
foo=""
if [ "" = "" ]
then
echo "Foo is blank"
fi
And that is valid. Another thing you can do is use the new test format that uses double square brackets:
foo=""
if [[ $foo = "" ]]
then
echo "Foo is blank"
fi
This will always work even without the extra quotes, and is now recommended unless you have to have your program compatible with the original Bourne shell syntax.
One more thing you can do in debugging your shell script is to use set -xv which turns on verbose debugging. Each statement, before it is executed will be printed, then it will print again after the shell fills in variables, patterns, etc., and then execute. It's a great way to debug your program. Just put set -xv on the line before you want this verbose debugging mode and use set +xv to turn it off. (Yes, the - turns it on and + turns it off.)
Thanks alot David, great answer, could you also tell me what the best way to get the && or equivalent of it within this as I need to find out if they are both bust, or just one etc
As already mentioned in a comment, you can use either one of these two forms:
if [ "$foo" = "bar" ] && [ "$bar" = "foo" ]
or
if [[ $foo = "bar" && $bar = "foo" ]]

test works with "$f" but not with "$#"

I was writing a little bash script and noticed an odd thing. Basically it's just when I execute this:
if [ "$#" = "spam with eggs" ]
then
echo "yay"
else
echo "nay"
fi
This gives the following error:
$ sh spam.sh spam with eggs
spam.sh: 3: [: =: unexpected operator
nay
But if I change the script to the following, it works:
f="$#"
if [ "$f" = "spam with eggs" ]
then
echo "yay"
else
echo "nay"
fi
Why doesn't it work with "$#" in the first place?
Because "$#" is very specifically rigged to preserve whitespace exactly as it was. In your example, it returns three strings, not one. You want "$*" here. (That's unusual; most people have the opposite problem.)

What is the simplest way to check if a character is found within a variable in BASH?

I need to check if a variable contains a particular character, for use in an if-conditional in BASH, e.g.:
if [ "①" is in "$numbers" ]
then
echo "Found."
else
echo "Not found."
fi
If $numbers is "These are some numbers 1232", it returns "Not found.", but if "①" is found anywhere in the line, it returns "Found."
I have been using $numbers | grep -c ①, then checking if the output is greater than "0", but it seems there must be a simpler solution.
Right hand side of a comparison can be a pattern:
if [[ $numbers = *①* ]] ; then
As long as it's bash and doesn't need to be posix:
if [[ "$numbers" =~ ① ]]; then
echo "Found"
fi
For a posix solution, use a case statement in place of an if statement:
numbers="①"
case "$numbers" in
*①*) echo "Found it." ;;
*) echo "Not here." ;;
esac
This solution will work under dash which is the default shell (/bin/sh) for scripts under Debian-influenced distributions.

Resources