This question already has answers here:
How do I test if a variable is a number in Bash?
(40 answers)
Closed 2 years ago.
#!/bin/bash
if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ]
then
echo This is empty or does not have all 3 parameters
exit
elif [ "$1" -lt 0 ]
then
echo This aint a number
exit
fi
Trying to run a script it is suppose first check if 3 positional parameters were entered,
secondly check if the input are numbers and then display the largest. I got the first if statement to work but when I input a string for the first parameter to test the elif statement an error that says integer expected.
you have to review the regex to check if string only contains digits, but you may try this:
#!/bin/bash
if [[ "$1" == "" ]] || [[ "$2" == "" ]] || [[ "$3" == "" ]]
then
echo "This is empty or does not have all 3 parameters"
exit
elif ! [[ "$1" =~ ^[0-9]+$ ]]
then
echo "This aint a number"
exit
fi
Note that [[ is actually a command/program that returns either 0 (true) or 1 (false). Any program that obeys the same logic (like all base utils, such as grep(1) or ping(1)) can be used as condition :
[[ -z STRING ]] #Empty string
[[ -n STRING ]] #Not empty string
[[ STRING == STRING ]] #Equal
[[ STRING != STRING ]] #Not Equal
[[ NUM -eq NUM ]] #Equal
[[ NUM -ne NUM ]] #Not equal
[[ NUM -lt NUM ]] #Less than
[[ NUM -le NUM ]] #Less than or equal
[[ NUM -gt NUM ]] #Greater than
[[ NUM -ge NUM ]] #Greater than or equal
[[ STRING =~ STRING ]] #Regexp
(( NUM < NUM )) #Numeric conditions
also to check if the string contains only digits/numerical cheracter
[[ "$1" =~ ^[0-9]+$ ]]
Please use a regex to check for number
#!/bin/bash
if [ $1 == "" ] || [ $2 == "" ] || [ $3 == "" ]
then
echo This is empty or does not have all 3 parameters
exit
elif ! [[ $1 =~ ^[0-9]+$ ]]
then
echo This aint a number
exit
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 am trying to code a script that will tell the user if a triangle is isosceles, equilateral, or scalene. The error is occuring in line 7 (The elif line)
#!/bin/bash
read -p "Enter a number: " x
read -p "Enter a number: " y
read -p "Enter a number: " z
let "a = x + y + z"
if [ $x -eq $y ] && [ $y -eq $z ]
then echo "EQUILATERAL"
elif [[[ $x -eq $y ] && [ $y != $z ]] || [[ $x -eq $z ] && [ $z != $y ]] || [[ $y -eq $z ] && [ $z != $x ]]]
then echo "ISOSCELES"
elif [ $a -gt 1000 ]
then echo "Cannot equal more than 1000"
fi
I do realize that I could do the same thing with multiple elif lines, but I also have another elif as well and I want to keep it clean. Thanks all!
It seems like you think square brackets in the shell are like parentheses in C-style programming languages. That's not how they work. [ is a synonym for the test command, the condition it introduces ends with ]. And [[ is a special token that introduces a conditional expression, which ends with ]]. You can't mix them up, you can't add additional brackets like [[[, and they don't nest.
The grouping operators in the shell are { ... } and ( ... ); the latter also creates a subshell.
elif ( [[ $x -eq $y ]] && [[ $y != $z ]] ) || ( [[ $x -eq $z ]] && [[ $z != $y ]] ) || ( [[ $y -eq $z ]] && [[ $z != $x ]] )
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.
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 can't seem to allow single integer input only.
If someone puts abc, it will work.
But if someone puts in abc123 or 123abc it will still treat it as a valid integer
# input to be an integer.
validate_integer(){
if [ ! "$#" -eq "1" ]; then
error "Please enter one numberic value only"
return 1
elif [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
error "Input must be a NUMBER"
return 1
else
return 0
fi
}
Change this line:
elif [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
to this:
elif ! [[ "$1" =~ ^[[:digit:]]+$ ]]; then
There, ^[[:digit:]]+$ means the string must be composed of digits from beginning to end, and it must be 1 or more characters long. We negate this with !, to handle the case when the string does not satisfy this condition (not fully numeric).