Why does if condition give error in bash? [duplicate] - windows

This question already has answers here:
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 6 years ago.
My code below:
echo "====================================="
echo " Test Programme "
echo "====================================="
echo
read -p "Enter Name: " name
if [$name -eq ""]; then
sleep 1
echo "Oh Great! You haven't entered name."
exit
fi
read -p "Enter age: " age
According to that code,I expected "Oh Great! You haven't entered name." to show up when user skips entering the name which WORKS WELL
But, when you enter a proper string for name, it gives this message/ error:
./cool_ham.sh: line 13: [Franco: command not found
I want to know the reason for that.
I have even tried "$name" = "" after #Jack suggested, but still din't work .

Put a space between the square braces of your if condition and its contents.
if [ "$name" = "" ]; then
Additionally note that I use = over -eq to compare strings. -eq is used to compare integer values, while = will compare strings, which can be unintuitive coming from other languages.
I also quoted $name to prevent globbing and word splitting.

Related

search for a variable in a string in bash [duplicate]

This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 6 months ago.
I have a string given below:
string1 = "Hello there, my name is Jack.
Hello there, my name is Jack.
Hello there, my name is Jack."
I'm taking the following input from the string:
read string2
I want to check whether the string2(which is a variable) is present in string1.
I tried running the below command:
output=$(echo $string1 | grep -o "$string2")
echo $output
eg: Let string2="name"
The output is empty when I'm running this command.
Can someone tell me where am I going wrong?
#!/bin/bash
string1="Hello there, my name is Jack"
string2="name"
if [[ $string1 == *"$string2"* ]]; then
echo "$string2 found"
else
echo "$string2 not found"
fi
Alternate method with POSIX-shell grammar:
string1='Hello there, my name is Jack'
string2='name'
case "$string1" in
*"$string2"*) printf '%s found\n' "$string2";;
*) printf '%s not found\n' "$string2";;
esac

The If Else comparsion throws weired error [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 8 months ago.
Unable to run code, error "[[Add:" not found.
#!/bin/bash
add() {
first="${1}"
second="${2}"
result=`expr $first + $second`
echo "The sum of two numbers are $result"
return $result
}
subtract() {
first="${1}"
second="${2}"
result=`expr $first - $second`
echo "The sum of two numbers are $result"
return $result
}
echo "[0] Add"
echo "[1] Subtract"
read operation
echo "Enter Number One"
read first
echo "Enter Number Two"
read second
if [["$operation" == "Add"]]
then
add $first $second
else
subtract $first $second
fi
I am trying to run this code, the if else comparison does not work. Each time i execute the code, at line 32 an error is occurred and else is executed. I am trying to create an addition/subtraction bash sh file.
root#Kumaraswamy:~# ./maths.sh
[0] Add
[1] Subtract
Add
Enter Number One
7
Enter Number Two
3
./maths.sh: line 32: [[Add: command not found
The sum of two numbers are 4
root#Kumaraswamy:~#
Leave a space after [ or [[. That should work since these are real executables found in one of the directories listed under environment variable $PATH.

Bash program exits when it runs no matter what [duplicate]

This question already has answers here:
Why equal to operator does not work if it is not surrounded by space?
(4 answers)
Why is [ "$foo"=="$bar" ] always true in bash? [duplicate]
(1 answer)
Closed 3 years ago.
I'm trying to have the script exit if the user enters 'q'
here's the code:
echo "Enter Choice => "
read target
if [[ $target=='q' ]]; then
exit 1
else
#do something
fi
However when I run it no matter what the input is, the script exits...
#!/bin/bash
echo "Enter Choice => "
read target
if [[ $target == 'q' ]]; then
exit 1
else
#do something
echo "do something..."
fi
~
Try this - spacing between the tokens $target, '==' and 'q' is important.

input vs variable comparison isn't working? [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 6 years ago.
Here is a random number generator followed by a line clear timer and you are then to try and input the number that you saw. I cant seem to set up the comparison between the randNum variable and the input. I tried setting a defined value for the input as well and still receive an error "command not found" when checking input vs randNum variable
n=$RANDOM$RANDOM$RANDOM; let "n %= 10000000000";
echo $n
for i in {5..1}; do echo $i; sleep 1; tput cuu1; tput el; done
tput cuu1; tput el
echo "what was that number?"
#read input
input=999999999
if [$input == $n]
then
echo "you are correct"
else
echo "you are incorrect"
fi
Shells need spaces around brackets [ and ] when used for this purpose (replacement syntax for the test command), and you should better use -eq instead of == for numeric comparison:
if [ $input -eq $n ]
If you use the arithmetic comparison, rather than the old testcommand (a.k.a. [) then you can avoid these issues. Spaces here are less important inside the brackets:
if (( input == n ))
Note that the leading $ is not required (and using it can cause issues)

bash: Can't get simple 'if' to work

I don't understand why this simple read doesn't work. Mind you, I am VERY new to bash. :)
#!/bin/bash
echo -n "Project Name: "
read PROJECT_NAME
if [ -n "$PROJECT_NAME" ]; then
echo "You must provide a project name."
exit 2
fi
-- snip --
When this executes, it asks for the project name. After I press enter, I get "You must provide a project name." and then the scripts exists instead of continuing.
What am I doing wrong?
Thanks
Eric
You want [ -z "$PROJECT_NAME" ], not -n:
From man test:
-n STRING
the length of STRING is nonzero
...
-z STRING
the length of STRING is zero
to avoid confusion from -n or -z , you can just use case/esac to compare strings
case "$PROJECT_NAME" in
"" ) echo "No";;
*) echo "Have";;
esac

Resources