Why bash consider every character as a number - bash

This is the code:
sum=0
i=1
while [ $i -le $# ]
do
if [[ $i =~ ^[0-9]+$ ]];
then
sum=$(($sum+$(eval echo '$'$(eval echo ${i}))))
echo "$(eval echo '$'$(eval echo $i)) is number"
i=$((i+1))
else
echo "$(eval echo '$'$(eval echo $i)) is not a number"
i=$((i+1))
fi
done
echo "sum is $sum"
and this is the output when I try to distinguish b/w numbers and characters:
$ bash sumOfGivenNums.sh 3 4 3 a
3 is number
4 is number
3 is number
a is number
sum is 10

You've got a lot of really unnecessary code in your script; you almost never want to use the eval function. I would probably write something like this:
sum=0
for arg in "$#"; do
if [[ $arg =~ ^[0-9]+$ ]]; then
echo "$arg is a number"
(( sum += arg ))
else
echo "$arg is not a number."
fi
done
echo "sum is $sum"

Related

Error getting in the second if statement in bash script

n=20
x=3
count=0
flag=0
i=1
declare -a arr[n+1]
for (( j=0;j<=n;j++ ))
do
arr+=(0)
done
#echo "${arr[#]}"
while [[ $count -ne $n ]]
do
if [[ $i -le $n ]]
then
if [[ ${arr[$i]} -eq '0' ]]
then
echo "Value is ${arr[$i]}"
#${arr[$(i-1)]}= (( ${arr[$i-1]++} ))
${arr[$i]}+=${arr[$i]}
echo " "
#echo -n "${arr[$i]}"
echo -n " $i"
count=$(( count+1 ))
i=$(( i+1+x ))
else
i=$(( i+1 ))
fi
else
i=$(( i-n ))
flag=$(( flag+1 ))
fi
done
echo " "
echo "No of round : $flag"
This is the whole code, I've tried to print numbers that follows this: n=20 is the number of elements and x=3 is the number that we have to avoid. For example,
20
3
1,5,9,13,17,2,6,10,14,18,3,7,11,15,19,4,8,12,16,20,
3
But, the problem is that my second if condition is not fulfilling, if ignores the condition. Above example is for the C++, but in bash script, 2nd if statement isn't working. This can be because syntax is wrong. So can you please help me to find the mistakes.
Output of the above code:
output
${arr[$i]}+=${arr[$i]}
This is incorrect. $ should not be used when you assign the value.
If you want to double the value, replace this string with the following:
arr[$i]=$(( ${arr[$i]} + ${arr[$i]} ))
Or what you want to do there?

Shell Script - Fibonacci

the code below cannot make the fibonacci sequence more than 93 sequences, how can i solve this? I would like you to do with any number
#!/bin/bash
clear
echo "Program to Find Fibonacci Series"
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
You can use the "bc" command (an interactive algebraic language with arbitrary precision) to get past numeric limits of the shell. Here is the re-write of your while loop:
while [[ $i -lt $n ]]
do
i=$(( $i + 1 ))
z=$( bc <<< "$x + $y" )
echo "$z"
x=$y
y=$z
done
On Debian/Ubuntu/RHEL/CentOS systems, install the optional "bc" package.

How to handle negative arguements in a fibonacci series using bash script in linux

How can I add a condition that will deal with a negative number input for my Fibonacci series in bash script? This is my code:
clear
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: Program that finds the fibonacci series"
echo "Options:"
echo "-h shows this help information "
echo "-lt the value on the left is less than the value on the right"
echo "expr evaluates a given expression and displays its corresponding output "
exit 1
fi
echo 'This program will find the Fibonnacci Series'
echo "Enter the number of terms you want to be generated"
read n
f1=0
f2=1
i=2
echo "The following is the Fibonacci Series upto $n term:"
echo $f1
echo $f2
while [ $i -lt $n ]
do
i=`expr $i + 1`
f3=`expr $f1 + $f2
echo $f3
f1=$f2
f2=$f3
done

Get user input and check it, linux

I am a beginner at unix and I am trying to use a while loop to get a user integer input for 2 numbers, but I need to check if it is an integer and reask if it's not, so I attempted to utilize if statements inside the while loop. I cannot get this to work, what am I doing wrong with the while and if loop?
#! /bin/bash
echo “Enter first number:“
while read number1
do
if[[ $number1 ]] && [ $input -eq $input 2>/dev/null ]
then
echo “$number1”
else
echo "$number1 is not an integer or not defined.Try again”
fi
done
echo “Enter second number:“
while read number2
do
if[[ $number2 ]] && [ $input -eq $input 2>/dev/null ]
then
echo “$number2”
else
echo "$number2 is not an integer or not defined.Try again”
fi
done
If you are trying to get the number and checking it until it becomes integer, Please try the below code.
#!/bin/bash
echo "enter number"
while read number1
do
if ! [[ $number1 =~ ^[0-9]+$ ]]
then
echo "number is not an integer"
else
echo "number is an integer"
exit;
fi
done

How to check if a number is within a range in shell

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

Resources