Hey so why am I getting a syntax error on my conditionals. I've checked with previous questions online on the use of quotes "" and [[]] and made sure to use both. Also the && symbol in the if statement but I can't seem to get it to work any ideas?
#!/bin/bash
echo Please input integer number 1
read i1
echo Please input integer number 2
read i2
echo Please input integer number 3
read i3
if [[ "$i1" >= "$i2" ]] && [[ "$i1" >= "$i3" ]];then
echo $i1
elif [[ "$i2" >= "$i1" ]] && [[ "$i2" >= "$i3" ]];then
echo $i2
else
echo $i3
fi
It should be -ge instead of >=
Try man test for more information
#!/bin/bash
echo Please input integer number 1
read i1
echo Please input integer number 2
read i2
echo Please input integer number 3
read i3
if [[ "$i1" -ge "$i2" ]] && [[ "$i1" -ge "$i3" ]];then
echo $i1
elif [[ "$i2" -ge "$i1" ]] && [[ "$i2" -ge "$i3" ]];then
echo $i2
else
echo $i3
fi
Related
This question already has answers here:
Difference between sh and Bash
(11 answers)
How to use double or single brackets, parentheses, curly braces
(9 answers)
Closed 3 months ago.
I am doing a simple if else program in bash.
I am trying to understand the difference between [ and (( and [[
this below code works fine
#!/bin/bash
echo "enter a number"
read num
if [ $num -ge 1 -a $num -lt 10 ]
then
echo "A"
elif [ $num -ge 10 -a $num -lt 90 ]
then
echo "B"
elif [ $num -ge 90 -a $num -lt 100 ]
then
echo "C"
else
echo "D"
fi
This code also works fine
#!/bin/bash
echo "enter a number"
read num
if (( num >= 1)) && ((num<10))
then
echo "sam"
elif ((num >= 10)) && ((num < 90 ))
then
echo "ram"
elif ((num >= 90)) && ((num <100))
then
echo "rahim"
else
echo "tara"
fi
but when i try to use [[ there is a problem
#!/bin/bash
echo "enter a number"
read num
if [[ "num" -ge "1" ]] && [[ "num" -lt "10" ]]
then
echo "A"
elif [[ "num" -ge "10" ]] && [[ "num" -lt "90" ]]
then
echo "B"
elif [[ "num" -ge "90" ]] && [[ "num" -lt "100" ]]
then
echo "C"
else
echo "D"
fi
sourav#LAPTOP-HDM6QEG8:~$ sh ./ifelse2.sh
enter a number
50
./ifelse2.sh: 4: [[: not found
./ifelse2.sh: 7: [[: not found
./ifelse2.sh: 10: [[: not found
./ifelse2.sh: 14: echo D: not found
can someone explain this,i tried without double quoting the variable too.
I want to just insert number between two values, and otherwise the script repeated until correct number.
This is my script and it does not work correctly:
validation(){
read number
if [ $number -ge 2 && $number -ls 5 ]; then
echo "valid number"
break
else
echo "not valid number, try again"
fi
}
echo "insert number"
validation
echo "your number is" $number
If you are using Bash, you are better off using the arithmetic expression, ((...)) for readability and flexibility:
if ((number >= 2 && number <= 5)); then
# your code
fi
To read in a loop until a valid number is entered:
#!/bin/bash
while :; do
read -p "Enter a number between 2 and 5: " number
[[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number"; continue; }
if ((number >= 2 && number <= 5)); then
echo "valid number"
break
else
echo "number out of range, try again"
fi
done
((number >= 2 && number <= 5)) can also be written as ((2 <= number <= 5)).
See also:
Test whether string is a valid integer
How to use double or single brackets, parentheses, curly braces
Your if statement:
if [ $number -ge 2 && $number -ls 5 ]; then
should be:
if [ "$number" -ge 2 ] && [ "$number" -le 5 ]; then
Changes made:
Quoting variables is considered good practice.
ls is not a valid comparison operator, use le.
Separate single-bracket conditional expressions with &&.
Also you need a shebang in the first line of your script: #!/usr/bin/env bash
if [ $number -ge 2 && $number -ls 5 ]; then
should be
if [[ $number -ge 2 && $number -le 5 ]]; then
see help [[ for details
Try bellow code
echo "Enter number"
read input
if [[ $input ]] && [ $input -eq $input 2>/dev/null ]
then
if ((input >= 1 && input <= 4)); then
echo "Access Granted..."
break
else
echo "Wrong code"
fi
else
echo "$input is not an integer or not defined"
fi
2 changes needed.
Suggested by Sergio.
if [ "$number" -ge 2 ] && [ "$number" -le 5 ]; then
There is no need of break. only meaningful in a for, while, or until loop
while :; do
read option
if [[ $option -ge 1 && $option -lt 4 ]]; then
echo "correct"
c
break
else
echo "Incorrect option selected,choose an option between [1-4]"
fi
done
I want to just insert number between two values, and otherwise the script repeated until correct number.
This is my script and it does not work correctly:
validation(){
read number
if [ $number -ge 2 && $number -ls 5 ]; then
echo "valid number"
break
else
echo "not valid number, try again"
fi
}
echo "insert number"
validation
echo "your number is" $number
If you are using Bash, you are better off using the arithmetic expression, ((...)) for readability and flexibility:
if ((number >= 2 && number <= 5)); then
# your code
fi
To read in a loop until a valid number is entered:
#!/bin/bash
while :; do
read -p "Enter a number between 2 and 5: " number
[[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number"; continue; }
if ((number >= 2 && number <= 5)); then
echo "valid number"
break
else
echo "number out of range, try again"
fi
done
((number >= 2 && number <= 5)) can also be written as ((2 <= number <= 5)).
See also:
Test whether string is a valid integer
How to use double or single brackets, parentheses, curly braces
Your if statement:
if [ $number -ge 2 && $number -ls 5 ]; then
should be:
if [ "$number" -ge 2 ] && [ "$number" -le 5 ]; then
Changes made:
Quoting variables is considered good practice.
ls is not a valid comparison operator, use le.
Separate single-bracket conditional expressions with &&.
Also you need a shebang in the first line of your script: #!/usr/bin/env bash
if [ $number -ge 2 && $number -ls 5 ]; then
should be
if [[ $number -ge 2 && $number -le 5 ]]; then
see help [[ for details
Try bellow code
echo "Enter number"
read input
if [[ $input ]] && [ $input -eq $input 2>/dev/null ]
then
if ((input >= 1 && input <= 4)); then
echo "Access Granted..."
break
else
echo "Wrong code"
fi
else
echo "$input is not an integer or not defined"
fi
2 changes needed.
Suggested by Sergio.
if [ "$number" -ge 2 ] && [ "$number" -le 5 ]; then
There is no need of break. only meaningful in a for, while, or until loop
while :; do
read option
if [[ $option -ge 1 && $option -lt 4 ]]; then
echo "correct"
c
break
else
echo "Incorrect option selected,choose an option between [1-4]"
fi
done
Here is my script:
age=119
if [[$age -gt 99 ]]; then
age_3digits=$age
elif [[$age -gt 9]]; then
age_3digits=0$age
else
age_3digits=00$age
fi
z_grid=${age_3digits}Ma.grd
echo $z_grip
output: 00119Ma.grd
how come?? I am new to bash, thanks so much
You need a space after [[ and before ]]. Change to:
if [[ $age -gt 99 ]]; then
age_3digits=$age
elif [[ $age -gt 9 ]]; then
age_3digits=0$age
else
age_3digits=00$age
fi
It's also better to use arithmetic expressions because it makes your code more readable, like this:
if (( age > 99 )); then
age_3digits=$age
elif (( age > 9 )); then
age_3digits=0$age
else
age_3digits=00$age
fi
im trying to do a Number Check not including decimals as well as within range check but it is not working
example of accepted numbers: 1, 10, 100
not accepted numbers: 1.1, 10.1, 100.1
echo "Qty Sold: "
read sold
if [[ "$sold" =~ `^([0-9])$` && "$sold" -gt 0 && "$sold" -lt 999 ]] ;
then
echo "ok"
else
echo "Error, Qty Sold Must be Positive Integer";
fi
Remove the backticks which exists around your regex and use + after [0-9] to match one or more digits.
if [[ "$sold" =~ ^[0-9]+$ && "$sold" -gt 0 && "$sold" -lt 999 ]] ;
Example:
$ sold=12
$ if [[ "$sold" =~ ^[0-9]+$ && "$sold" -gt 0 && "$sold" -lt 999 ]] ; then echo 'Ok'; else echo 'NOT ok'; fi
Ok
$ sold=0
$ if [[ "$sold" =~ ^[0-9]+$ && "$sold" -gt 0 && "$sold" -lt 999 ]] ; then echo 'Ok'; else echo 'NOT ok'; fi
NOT ok
$ sold=1000
$ if [[ "$sold" =~ ^[0-9]+$ && "$sold" -gt 0 && "$sold" -lt 999 ]] ; then echo 'Ok'; else echo 'NOT ok'; fi
NOT ok
Without using regex:
((sold>0 && sold <999)) 2>/dev/null && echo "ok" || echo "Error, Qty Sold Must be Positive Integer"
The [[ syntax is a bit different from [ syntax. You do not need any delimiter around the regex in =~. So no / or backtick or apostrophe or ampersand is needed (only if regex contains a space. It can be escaped by backslash or add apostrophe or ampersand around the space or use [[:space:]] instead (which also includes TAB and some other whitespace characters)). What is more the ampersand also not needed around $sold (only in [[!). So the conditional part may be like this
if [[ $sold =~ ^[[:digit:]]{1,3}$ && $sold -gt 0 && $sold -lt 999 ]]; then echo ok
else echo "Error, Qty Sold Must be Positive Integer between 1 and 998";
fi
This checks if one, two or three digits are entered and it is not zero.