It seems that these two operators are pretty much the same - is there a difference? When should I use = and when ==?
You must use == in numeric comparisons in (( ... )):
$ if (( 3 == 3 )); then echo "yes"; fi
yes
$ if (( 3 = 3 )); then echo "yes"; fi
bash: ((: 3 = 3 : attempted assignment to non-variable (error token is "= 3 ")
You may use either for string comparisons in [[ ... ]] or [ ... ] or test:
$ if [[ 3 == 3 ]]; then echo "yes"; fi
yes
$ if [[ 3 = 3 ]]; then echo "yes"; fi
yes
$ if [ 3 == 3 ]; then echo "yes"; fi
yes
$ if [ 3 = 3 ]; then echo "yes"; fi
yes
$ if test 3 == 3; then echo "yes"; fi
yes
$ if test 3 = 3; then echo "yes"; fi
yes
"String comparisons?", you say?
$ if [[ 10 < 2 ]]; then echo "yes"; fi # string comparison
yes
$ if (( 10 < 2 )); then echo "yes"; else echo "no"; fi # numeric comparison
no
$ if [[ 10 -lt 2 ]]; then echo "yes"; else echo "no"; fi # numeric comparison
no
There's a subtle difference with regards to POSIX. Excerpt from the Bash reference:
string1 == string2
True if the strings are equal. = may be used in place of == for strict POSIX compliance.
Related
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
Hello I am wondering if anyone can help me :
I try to make a multiplication table for my daughter, I don't know how to make it. This is how I want to look :
1 x 1 = ? answer
if the answer is true then go to the next one
1 x 2 = ?
but if the answer is false then ask again 1 x 1 = ?
until the answer is Correct.
#!/bin/bash
# Multiplication table
echo " --== Multiplication Table ==-- "
sleep 2
echo " Lesson 1"
sleep 1
echo ""
echo -n "1 x 1 = ? " ; read opt
if [ "$opt" = 1 ]
then
echo "Correct!"
else
echo "Wrong!"
fi
sleep 1
echo ""
echo -n "1 x 2 = ? " ; read opt
if [ "$opt" = 2 ]
then
echo "Correct!"
else
echo "Wrong!"
fi
After the exercise is done until 10. Then show a result of how many correct answers has and how many Wrong.
Example:
Lesson 1 is finish you have 9 correct answers and 1 wrong answer !
You can use this script:
#!/bin/bash
# Multiplication table
echo " --== Multiplication Table ==-- "
sleep 2
echo " Lesson $1"
sleep 1
echo ""
wrong=0
correct=0
for i in {1..10}
do
while true
do
echo -n "$1 x $i = ? " ; read opt
if [ $opt = $(( $1 * $i )) ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
done
echo "Lesson $1 is finish you have $correct correct answers and $wrong wrong answer!"
You can run it with different params for any base. Such as ./script 5 for multiplication with 5.
I made it. This is how I want to look like and work
#!/bin/bash
# Multiplication table
echo " --== Multiplication Table ==-- "
sleep 2
echo " Lesson 1"
sleep 1
echo ""
while true
do
echo -n "1 x 1 = ? " ; read opt
if [ $opt = 1 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 2 = ? " ; read opt
if [ $opt = 2 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 3 = ? " ; read opt
if [ $opt = 3 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 4 = ? " ; read opt
if [ $opt = 4 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 5 = ? " ; read opt
if [ $opt = 5 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 6 = ? " ; read opt
if [ $opt = 6 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 7 = ? " ; read opt
if [ $opt = 7 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 8 = ? " ; read opt
if [ $opt = 8 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 9 = ? " ; read opt
if [ $opt = 9 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
while true
do
echo -n "1 x 10 = ? " ; read opt
if [ $opt = 10 ]
then
(( correct++ ))
echo "Correct!"
break
else
(( wrong++ ))
echo "Wrong!"
fi
done
echo "Lesson 1 is finish you have $correct correct answers and $wrong wrong answer!"
#!/bin/bash
#######multiplication table#########
INPUT_NUMBER=$1
if [[ $# -ne 1 ]]; then
echo "please enter at least 1 number"
exit 1
fi
if [[ ! ${INPUT_NUMBER} =~ [[:digit:]] ]];then
echo "Incorrect usage, wrong type args"
echo "Usage is $0 <number>"
exit 1
fi
for i in {1..10}
do
echo "${INPUT_NUMBER} * ${i} = $((${INPUT_NUMBER} * ${i}))"
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, im having trouble in elif statement. I need it to exit if user enters a non negative integer as argument 1 or a string..
#!/bin/sh
n="0"
m="$1"
if test $# != "2"
then
echo "Usage: ./echon.sh <number of lines> <string>"
exit 1
elif [ $1 -eq "[^0-9]" ] || [ $1 = "[a-zA-Z]" ]
then
echo "./echon.sh: argument 1 must be a non-negative integer"
exit 1
else
while [ "$n" -lt "$m" ]
do
echo "$2"
n=$(($n + 1))
done
fi
A quick rewrite
#!/bin/bash
shopt -s extglob
if (( $# != 2 )); then
echo "Usage: $0 <number of lines> <string>"
elif [[ $1 != 0 && $1 != [1-9]*([0-9]) ]]; then
echo "$0: argument 1 must be a non-negative integer"
else
n=0
while (( n < $1 )); do
echo "$2"
((n++))
done
fi
Notes
using [[ ... ]] for string comparisons
using (( ... )) for arithmetic expressions
using extended globbing to match a non-negative integer (enforcing a non-zero number does not begin with 0, to avoid potential invalid octal numbers)
Replace your elif line with the following:
elif echo $1 | grep -qv "^[0-9][0-9]*"
I am writing a very simple comparison in shell bash script, but I never get it correct:
count=0
if [ expr $count / 4 = 0 ];
then
echo "yes";
else
echo "no";
fi
always giving no?
If you want to call out to the expr program, you have to actually call out to it:
if [ $(expr $count / 4) = 0 ]; then echo "yes"; else echo "no"; fi
However, bash can do it in-house:
if (( $count / 4 == 0 )); then echo "yes"; else echo "no"; fi
You need to use command substitution ($() or backticks) to evaluate the eval expression. Also, use -eq for integer comparison:
if [ $(expr $count / 4) -eq 0 ];
then
echo "yes";
else
echo "no";
fi
How about this
[[ count/4 -eq 0 ]] && echo 'yes' || echo 'no'