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
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.
Im trying to get an array from grades.txt, and determine what letter grade it should be assigned.
I either get
hw4part2.sh: line 26: [: : integer expression expected
If i use -ge or
hw4part2.sh: line 26: [: : unary operator expected
If i use >=
Below is the code im trying to get working
mapfile -t scores < grades.txt
numOScores=0
numOA=0
numOB=0
numOC=0
numOD=0
numOF=0
DoneWScores=0
A=90
B=80
C=70
D=60
F=59
while [ $DoneWScores -eq 0 ]
do
numOScores=$((numOScores + 1))
if [ "${scores[$numOScores]}" -ge "$A" ]
then
echo "A"
elif [ "${scores[$numOScores]}" -ge "$B" ]
then
echo "B"
elif [ "${scores[$numOScores]}" -ge "$C" ]
then
echo "C"
elif [ "${scores[$numOScores]}" -ge "$D" ]
then
echo "D"
elif [ "${scores[$numOScores]}" -le "$F" ]
then
echo "F"
else
echo "Done/error"
DoneWScores=1
fi
done
If anyone knows what my problem is, that'd be greatly appreciated
Consider this:
#!/usr/bin/env bash
if (( ${BASH_VERSINFO[0]} < 4 )); then
echo "Bash version 4+ is required. This is $BASH_VERSION" >&2
exit 1
fi
letterGrade() {
if (( $1 >= 90 )); then echo A
elif (( $1 >= 80 )); then echo B
elif (( $1 >= 70 )); then echo C
elif (( $1 >= 60 )); then echo D
else echo F
fi
}
declare -A num
while read -r score; do
if [[ $score == +([[:digit:]]) ]]; then
grade=$(letterGrade "$score")
(( num[$grade]++ ))
echo "$grade"
else
printf "invalid score: %q\n" "$score"
fi
done < grades.txt
for grade in "${!num[#]}"; do
echo "$grade: ${num[$grade]}"
done | sort
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
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