Bash function to execute a git pull in all subfolder where a .git directory is found.
I'm having issues with the last elif.
This part:
...
elif [ $answer == '' || $answer -ne 'y' || $answer -ne 'n' ] ; then
echo '---Please answer with y/n---'
...
Whole code of the function:
dogitpull () {
for i in */.git; do ( echo $i; cd $i/..; ); done
echo -n 'Are you sure? (y/n) '
read answer
echo $answer
if [ $answer == 'n' ] ; then
echo '---CANCELED---'
elif [ $answer == 'y' ] ; then
echo '---------------------------'
for d in */.git; do ( echo $d; cd $d/..; git pull; echo '---------------------------'; ); done
elif [ $answer == '' || $answer -ne 'y' || $answer -ne 'n' ] ; then
echo '---Please answer with y/n---'
fi
}
If the answer is empty (pressed enter), is not 'y' or is not 'n' there should be the text "Please answer with y or n" and should start again from the "read answer".
Does anybody have a solution to this?
Output: when pressing enter
-bash: [: ==: unary operator expected
-bash: [: ==: unary operator expected
-bash: [: missing `]'
-bash: -ne: command not found
-bash: -ne: command not found
Output: when entering something else than y or n
-bash: [: missing `]'
-bash: a: command not found
-bash: a: command not found
use case. example:
#!/bin/bash
echo -n 'Are you sure? (y/n) '
read answer
echo $answer
case "$answer" in
n|N) echo '---CANCELED---';;
y|Y) echo '-- yes ---';;
*) echo '---Please answer with y/n---';;
esac
The problem here is that you didn't quote the variable. When $answer is empty then [ $answer == '' ] expands to [ == '' ] which gives an error since there is nothing on the left hand side of the ==.
Fix
Quote your your variable and then proceed to fix the following errors (courtesy of Kamil Cuk):
-ne is for numbers, it will error with "Integer expression expected". Use != instead.
|| is not valid for [. Use -o or put the || outside the braces: [ ... ] || [ ... ] || [ .. ]
elif [ "$answer" = '' ] || [ "$answer" != 'y' ] || [ "$answer" != 'n' ]
Improvements
Use a single = instead of ==. [ a = b] is the official and portable way to check equality of a and b.
Instead of elif [ "$answer" == '' || "$answer" -ne 'y' || "$answer" -ne 'n' you may want to write just else which has the samme effect due to the preceding if-cases.
if [ "$answer" = 'n' ] ; then
# ...
elif [ "$answer" = 'y' ] ; then
# ...
else
# ...
fi
If answer is not a y and it is not an n, then you need to take action.
elif [ "$answer" != 'y' ] && [ "$answer" != 'n' ]; then
echo '---Please answer with y/n---'
Or you can do Moore's law and:
If not both: answer is 'Y' or answer is 'n', then you need to take action.
elif ! { [ "$answer" = 'y' ] || [ "$answer" = 'n' ]; }; then
echo '---Please answer with y/n---'
or with a ( subshell:
elif ! ( [ "$answer" = 'y' ] || [ "$answer" = 'n' ] ); then
echo '---Please answer with y/n---'
Related
Here is my code
#! /bin/bash
read var
if [ $var="Y" -o $var="y" ]
then
echo "YES"
else
echo "NO"
fi
I want to print YES if the user presses y or Y, otherwise I want to print NO. Why doesn't this code work?
Basically, your Condition is wrong. Quote your variables and leave spaces between operators (like shellter wrote). So it should look like:
#! /bin/bash
read var
if [ "$var" = "Y" ] || [ "$var" = "y" ]
then
echo "YES"
else
echo "NO"
fi
Edit: for POSIX ccompatibility
Replaced == with = - see comments
Replaced -o syntax with || syntax - see comments
With Bash, you can also use regular expression in your test with the =~ operator:
read var
[[ "$var" =~ [Yy] ]] && echo "YES" || echo "NO"
Or as Benjamin W. mentionned, simply use character range with the == operator:
read var
[[ "$var" == [Yy] ]] && echo "YES" || echo "NO"
There is minor syntax error in your code.
Correction : There should be a white space between operators and variables
read var
if [ $var = "Y" -o $var = "y" ]
then
echo "YES"
else
echo "NO"
fi
Try the above bash script.
Hope it would work fine.
Happy Coding!
If all you require is a upper/lowercase comparison, use the ,, operator on the variable being compared ( note the ${var,,} ):
#!/bin/bash
read var
if [ ${var,,} = "y" ]
then
echo "YES"
else
echo "NO"
fi
or more succinctly:
#!/bin/bash
read var
[ ${var,,} = 'y' ] && echo 'YES' || echo 'NO'
or the way I might actually do it:
#!/bin/bash
read var
[[ "${var,,}" == 'y' ]] && echo 'YES' || echo 'NO'
Below is the code that I tried.
#! /bin/bash
read -p "Are you Sure?(Y/N) " answer
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
echo "Do your stuff."
else
echo "Do your other stuff"
fi
Add whitespace around '=' and your code will run fine.
#! /bin/bash
read var
if [ $var = "Y" -o $var = "y" ]
then
echo "YES"
else
echo "NO"
fi
Try this code.
#! /bin/bash
read var
echo -e "YES\nNO\n" | grep -i $var
Can someone tell me why this script isnt working? I'm getting
./FileDirTest.sh: line 10: [: missing `]'
./FileDirTest.sh: line 10: n: command not found
./FileDirTest.sh: line 13: [: missing `]'
./FileDirTest.sh: line 13: n: command not found
Here is my script.
if [ -d "$PASSED1" ]
then echo "Do you want to execute whole directory?(Y/N)"
read answer
if [ "$answer" == "y" || "$answer" == "Y" ] ;
then echo "Execute"
fi
if [ "$answer" == "n" || "$answer" == "N" ] ;
then echo "No"
exit 1
fi
fi
Im sure it is something simple. I new to all of this.
|| is not a valid operator for the [ command; you can only use it to join two distinct [ commands:
if [ "$answer" = "y" ] || [ "$answer" = "Y" ];
You can, however, use || inside bash's conditional command:
if [[ "$answer" = "y" || "$answer" = "Y" ]];
The first of the two errors occurs because ||, being a special shell operator, indicates that the previous command is complete, but [ requires ] be given as the final argument. The second error occurs because the value of $answer, immediately following ||, is taken as the name of the command to run.
In addition to #Chepner's answer, you can also use, bash -o operator,
if [ "$answer" == "y" -o "$answer" == "Y" ]; then
echo "Execute"
else
echo "No"
exit 1
fi
When I run this bash script :
if [ [$EUID -ne 0] ]; then
echo "This script must be run as root" 1>&2
exit 1
else
printf " whathever "
exit 0
fi
I have this error :
./myScript: 15: [: Illegal number: [
Do you see any problem ?
You have syntax error in your if condition, use this if condition:
if [ "$EUID" -ne 0 ];
OR using [[ and ]]
if [[ "$EUID" -ne 0 ]];
You have syntax error in your if condition, use this if condition:
if [ "$EUID" -ne 0 ];
OR using [[ and ]]
if [[ "$EUID" -ne 0 ]];
If you use the KSH88+/Bash 3+ internal instruction [[, it's not necessary to use doubles quotes around the variables operands :
[ ~/test]$ [[ $var2 = "string with spaces" ]] && echo "OK" || echo "KO"
OK
Instead of the external command test or his fork [ :
[ ~/test]$ [ $var2 = "string with spaces" ] && echo "OK" || echo "KO"
bash: [: too many arguments
KO
[ ~/test]$ [ "$var2" = "string with spaces" ] && echo "OK" || echo "KO"
OK
Of course, you also have to choose the operators according to the type of operands :
[ ~/test]$ var1="01"
[ ~/test]$ [ "$var1" = "1" ] && echo "OK" || echo "KO"
KO
[ ~/test]$ [ "$var1" -eq "1" ] && echo "OK" || echo "KO"
OK
two suggestions apart from what everyone else has pointed out already.
rather than doing else [bunch of code because we are root] fi, just replace the else with fi. once you've tested for the failure condition you are concerned about and taken appropriate action, no need to continue to be within the body of the conditional.
$EUID is a bashism, if you would like to make this portable to shells such as ksh, replacing it with:
if [ $(id -u) -ne 0 ]; then echo "ur not root bro"; exit 1; fi
would be a good way to do it.
using
sudo bash shell_script.sh
instead of
sudo sh shell_script.sh
solved in my case.
I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script:
my_error_flag=0
my_error_flag_o=0
do something.....
if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then
echo "$my_error_flag"
else
echo "no flag"
fi
Basically, it should be, something along:
if ((a=1 or b=2) or (a=1 and b=2))
then
display error
else
no error
fi
The error I get is:
line 26: conditional binary operator expected
line 26: syntax error near `]'
line 26: `if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then'
Are my brackets messed up?
Use -a (for and) and -o (for or) operations.
tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
Update
Actually you could still use && and || with the -eq operation. So your script would be like this:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 ]); then
echo "$my_error_flag"
else
echo "no flag"
fi
Although in your case you can discard the last two expressions and just stick with one or operation like this:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ]; then
echo "$my_error_flag"
else
echo "no flag"
fi
You can use either [[ or (( keyword. When you use [[ keyword, you have to use string operators such as -eq, -lt. I think, (( is most preferred for arithmetic, because you can directly use operators such as ==, < and >.
Using [[ operator
a=$1
b=$2
if [[ a -eq 1 || b -eq 2 ]] || [[ a -eq 3 && b -eq 4 ]]
then
echo "Error"
else
echo "No Error"
fi
Using (( operator
a=$1
b=$2
if (( a == 1 || b == 2 )) || (( a == 3 && b == 4 ))
then
echo "Error"
else
echo "No Error"
fi
Do not use -a or -o operators Since it is not Portable.
Please try following
if ([ $dateR -ge 234 ] && [ $dateR -lt 238 ]) || ([ $dateR -ge 834 ] && [ $dateR -lt 838 ]) || ([ $dateR -ge 1434 ] && [ $dateR -lt 1438 ]) || ([ $dateR -ge 2034 ] && [ $dateR -lt 2038 ]) ;
then
echo "WORKING"
else
echo "Out of range!"
You can get some inspiration by reading an entrypoint.sh script written by the contributors from MySQL that checks whether the specified variables were set.
As the script shows, you can pipe them with -a, e.g.:
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
...
fi
I'm trying to write a script which will read two choices, and if both of them are "y" I want it to say "Test Done!" and if one or both of them isn't "y" I want it to say "Test Failed!"
Here's what I came up with:
echo "- Do You want to make a choice?"
read choice
echo "- Do You want to make a choice1?"
read choice1
if [ "$choice" != 'y' ] && [ "$choice1" != 'y' ]; then
echo "Test Done!"
else
echo "Test Failed!"
fi
But when I answer both questions with "y" it's saying "Test Failed!" instead of "Test Done!". And when I answer both questions with "n" it's saying "Test Done!"
What have I done wrong?
You are checking for the wrong condition.
if [ "$choice" != 'y' ] && [ "$choice1" != 'y' ];
The above statement is true when choice!='y' and choice1!='y', and so the program correctly prints "Test Done!".
The corrected script is
echo "- Do You want to make a choice ?"
read choice
echo "- Do You want to make a choice1 ?"
read choice1
if [ "$choice" == 'y' ] && [ "$choice1" == 'y' ]; then
echo "Test Done !"
else
echo "Test Failed !"
fi
The program is doing exactly what you told it to do. You said "If the first choice is not equal to 'y' and the second choice is not equal to 'y' then print "Test Done !" otherwise print "Test Failed !" -- so only if both choices are not y will "Test Done !" be printed.
You probably meant:
echo "- Do You want to make a choice ?"
read choice
echo "- Do You want to make a choice1 ?"
read choice1
if [ "$choice" == 'y' ] && [ "$choice1" == 'y' ]; then
echo "Test Done !"
else
echo "Test Failed !"
fi
I changed != not equals to == equals. Now only if you answer "y" to both questions will "Test Done !" be printed.
if [ "$choice" != 'y' -a "$choice1" != 'y' ]; then
echo "Test Done !"
else
echo "Test Failed !"
fi
You got the comparison logic backwards; from your description you wanted to say
if [ "$choice" = 'y' ] && [ "$choice1" = 'y' ]; then
I'm actually surprised that the && construct works, although on further inspection it probably should. Still, I would write it as
if [ "$choice" = 'y' -a "$choice1" = 'y' ]; then
You have your logic reversed; you're checking for != when you should be checking for ==. Try this:
if [ "$choice" == 'y' ] && [ "$choice1" == 'y' ]; then
echo "Test Done !"
else
echo "Test Failed !"
fi
Another thought,
$ c1='y' ; c2='y' ; [[ ${c1} = 'y' ]] && [[ ${c2} = 'y' ]] && echo true || echo false
true
$ c1='n' ; c2='y' ; [[ ${c1} = 'y' ]] && [[ ${c2} = 'y' ]] && echo true || echo false
false
$ c1='n' ; c2='y' ; [[ ${c1} = 'y' ]] || [[ ${c2} = 'y' ]] && echo true || echo false
true
$ c1='n' ; c2='n' ; [[ ${c1} = 'y' ]] || [[ ${c2} = 'y' ]] && echo true || echo false
false
$
Overflow of gibberish. (;
Try:
if [[ "$choice" != 'y' && "$choice1" != 'y' ]]; then
echo "Test Done!"
else
echo "Test Failed!"
fi
The line
if [ "$choice" != 'y' ] && [ "$choice1" != 'y' ]; then
tests if both choices aren't 'y', so when both choices are 'y', the statement is false and your program correctly prints "Test Failed".