I'm trying to get a simple while loop working in bash that uses four conditions, but after trying many different syntax from various forums, I can't find solution.
When i write 'Prod' or 'Dev' or 'Admin', i stay in the loop.
while [ -z $vmProfil ] || [ $vmProfil != 'Prod' ] || [ $vmProfil != "Dev" ] || [ $vmProfil != "Admin" ]
do
read -p 'Choose vm profil between Prod, Dev or Admin :' vmProfil
done
You are using || where you should be using &&; no matter what value vmProfil has, at least on of the != conditions must be true.
while [ -z "$vmProfil" ] || { [ "$vmProfil" != 'Prod' ] && [ "$vmProfil" != "Dev" ] && [ "$vmProfil" != "Admin" ]; }
You can also check negate the result of checking if any condition is true.
while [ -z "$vmProfil" ] || ! { [ "$vmProfil" = 'Prod' ] || [ "$vmProfil" = "Dev" ] || [ "$vmProfil" = "Admin" ]; }
I would write this as an infinite loop with an explicit break, though.
while :; do
read -p '...' vmProfil
case $vmProfil in
Prod|Dev|Admin) break ;;
esac
done
Related
I'm writing a script that checks for 3 different variables and matches on true. However I need it to only return true if ALL conditions match. Right now its returning true if only one or 2 conditions match from my list of operators.
if [ -z "$VAR3" ] || [ "$VAR1" -ge 10 ] || [ "$VAR2" != "WORKING" ]; then
echo "app failed "$VAR1" check-ins"
exit 2
elif [ -n "$VAR3" ] || [ "$VAR1" -le 10 ] || [ "$VAR2" == "WORKING" ]; then
echo "$VAR3" pid active connection is "$VAR2"
exit 0
fi
Should I just replace the || with && ??
Yes, || represents the OR logical operator, it returns true if at least one of the operators is true. && represents the AND logical operator, it returns true if and only if all of the operators are true
if [ -z "$VAR3" ] && [ "$VAR1" -ge 10 ] && [ "$VAR2" != "WORKING" ]; then
echo "app failed "$VAR1" check-ins"
exit 2
elif [ -n "$VAR3" ] && [ "$VAR1" -le 10 ] && [ "$VAR2" == "WORKING" ]; then
echo "$VAR3" pid active connection is "$VAR2"
exit 0
fi
What I want is
if((TRAVIS_BRANCH != "master") || (TRAVIS_BRANCH == "master" && TRAVIS_PULL_REQUEST == true)
How do I do this in bash? I think this is the closest, but it still doesn't work
if [ "$TRAVIS_BRANCH" != "master" ] || ["$TRAVIS_BRANCH" == "master" && "$TRAVIS_PULL_REQUEST" == "true"]
Make sure the [ and ] characters are surrounded by spaces
Double equals works in Bash, but is not POSIX compatible
You have to use -a, not &&, inside [
Because of the difficulty in working out the precedence rules in shell code it's recommended not to use more than one boolean operator per line of code
It's recommended to use [[ rather than [ in Bash
Result:
is_master_pull_request() {
[[ "$TRAVIS_BRANCH" = "master" ]] && [[ "$TRAVIS_PULL_REQUEST" = "true" ]]
}
if [[ "$TRAVIS_BRANCH" != "master" ]] || is_master_pull_request
Put spaces around your command names ([ is a command name), don't try to use && or || within a single [ command.
if [ "$TRAVIS_BRANCH" != "master" ] || { [ "$TRAVIS_BRANCH" = "master" ] && [ "$TRAVIS_PULL_REQUEST" = "true"]; }
However, you can use && or || inside of [[ ]], and also have less need to quote there:
if [[ $TRAVIS_BRANCH != master ]] || [[ $TRAVIS_BRANCH = master && $TRAVIS_PULL_REQUEST = true ]]
try:
if [ "$TRAVIS_BRANCH" != "master" ] || [[ "$TRAVIS_BRANCH" == "master" && "$TRAVIS_PULL_REQUEST" == "true" ]]; then
I know how to check for a file in bash using this code
file=$1
if [ -f "$file" ]
then
...
fi
But I want to do something when it's not a file.
file=$3
if [ "$1" == "" ] || [ "$2" == "" ] || [ $file is not a file??? ]
then
echo "use: notEmpty notEmpty file"
fi
Can anyone help me out?
if [ "$1" == "" ] || [ "$2" == "" ] || [ ! -f "$file" ]
The whitespaces after [ and before ] are important.
I am trying to write a script but it is giving the above error
if [ [ [ "$1" != "abc" ] && [ "$1" != "def" ] ] || [ [ "$2" != "1" ] && [ "$2" != "0" ] ] ];
then
echo "Hello World"
fi
Be careful with && and ||. You can simplify it to this in BASH:
if [[ "$1" != "abc" && "$1" != "def" ]] || [[ "$2" != "1" && "$2" != "0" ]];
then
echo "Hello World"
fi
I'm trying to check if both conditions return the expected values.
I want to be sure that both return the expected value before continuing...
My problematic line is: if [ [ $ansmob = "y" ] || [ $flagbook != "1" ] ];
read -r -p "Would you like to add $site.booking.local as well? [y/n] " ansbook
if [ $ansbook = "y" ];
then sed "s/ServerAlias.*/& $site.booking.local/" -i $workdir/$site$dom.conf
flagbook="1"
fi
read -r -p "Would you like to add m.$site.booking.local? [y/n] " ansmob
if [ [ $ansmob = "y" ] || [ $flagbook != "1" ] ];
then sed "s/& $site.booking.local/& $site.booking.local m.$site.booking.local/" -i $workdir/$site$dom.conf
else
sed "s/ServerAlias.* /& m.$site.booking.local/" -i $workdir/$site$dom.conf
flagmobile="1"
fi
Replace
if [ [ $ansmob = "y" ] || [ $flagbook != "1" ] ];
with
if [ "$ansmob" = "y" ] || [ "$flagbook" != "1" ]
with bash's double brackets, you can use && and ||
if [[ $ansmob = "y" || $flagbook -ne 1 ]]
Within double brackets, it's not strictly necessary to quote the variables: this command is smart about evaluating expressions with empty variables.
The binary && operator is the syntax for the AND operation.