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

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.

Related

bash script else block not working in Airflow [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 27 days ago.
I tried to implement the if else condition in Airflow, if a condition is true then statements under this condition will execute. In the else part the statements will not execute and return the task as failed
"a=0;"
"if [[$a -ge "1"]] ; then"
" echo 'Job Success';"
"else "
" echo 'Need Investigation';"
" exit 0;"
"fi;"
Here, If a is equal to 1 then the echo statement 'Job Success' is printed
If a is equal to 0 then the echo statement is not printed and the respective Airflow Dag is marked as failed
The [[ is a shell command (technically, a keyword) and requires whitespace between it and its arguments. You might be seeing an error [[0: command not found.
Also, the "inner" quotes aren't escaped (bash doesn't need them here):
"if [[ $a -ge 1 ]]; then"

How to only accept input that is a valid index of an array in bash [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Difference between single and double square brackets in Bash
(7 answers)
Why is "[[ 10 < 2 ]]" true when comparing numbers in bash? [duplicate]
(1 answer)
Closed 6 months ago.
I am trying some simple input validation in bash. Basically this part of my script outputs "press [index] to select [array element]." However I cannot seem to get it to stop and exit gracefully when an invalid input is entered. From my research so far this SHOULD work:
declare -a scenarios=()
scenarios+=("Scenario_123")
scenarios+=("Scenario_456")
scenarios+=("Scenario_789")
for i in ${!scenarios[#]}; do
echo -e "select $i for ${scenarios[$i]}"
done
read ScenInd
echo ${#scenarios[#]} #[${ScenInd}=~^[0-9]+$, =~ ^[[:digit:]]+
if ! [${ScenInd}=~ ^[[:digit:]]+$ ] || [${ScenInd} < 0] || [${ScenInd} >= ${#scenarios[#]}];
then
echo "INVALID SELECTION"
exit
fi
but when I run it and enter 8, I get '[8=~: command not found'
What have I done wrong and how do I fix this? I have tried this both with [${ScenInd}=~^[0-9]+$ and =~ ^[[:digit:]]+ yet the results are the same.
Thanks in advance

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

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.

bash script if and while conditions [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I am having trouble with executing the following bash script:
#!/bin/bash
response=" "
while ["$response" != "q"]; do
echo -n "Please enter a response"; read response
done
ALSO
!/bin/bash
response="x"
if ["$response" = "x"]
then
echo "the value is x"
fi
What could the possible errors be?
Your while and if statements are spaced wrong.
while [ "$response" != "q" ]; do etc
You need a space between the bracket and the double quote.

Bash Scripting if statement [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 8 years ago.
I don't know why this isn't working. Please help!
#!/bin/bash
clear
echo "Enter an option"
read $option
if ("$option" == 1) then
echo "Blah"
fi
I tried like this
if ("$option" -eq 1) then
I can't see why the if statement isn't being run. All I want to do is check what the user entered and do something depending on the value entered.
The syntax for an equality check is:
if [[ $option == 1 ]]; then
echo "Blah"
fi
Or, for compatibility with older non-bash shells:
if [ "$option" = 1 ]; then
echo "Blah"
fi
In either one, the whitespace is important. Do not delete the spaces around the square brackets.
That is not the syntax for an if statement in bash. Try this:
if [ "$option" = "1" ]; then
echo "Blah"
fi
I'm not sure where you got your syntax from...try this:
if [ "$option" -eq 1 ]; then
In the shell, [ is a command, not a syntactic construct.
Alternatively, you can use an arithmetic context in bash:
if (( option == 1 )); then
Bash if uses [ or [[ as test constructs, instead of parenthesis which are used in other languages.

Resources