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.
Related
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?
tableGenerator () {
if [ "$1" = "**" ];then
i=1
while [ $i -le 15 ]
do
echo "$number $1 $i =$(( $number $1 $i))"
i=$(($i+1))
done
else
i=1
while [ $i -le 15 ]
do
echo -n "$number $1 $i="
echo " scale=2 ;$number $1 $i " | bc
i=$(($i+1))
done
fi
So in the rest of the code i have the user select the number and operator, then I call the function passing in the user input. Now my question is , can i condense this function - using the same operation to handle floats and exponents??
I suppose the reason why you want to use bc in some cases is to have two decimals after the decimal point. In bc, the exponent operator is not ** as in bash but ^.
If these assumptions are correct, I would suggest you next code:
tableGenerator () {
if [ "$1" = "**" ]; then
op=^
else
op="$1"
fi
for i in {1..15}
do
echo -n "$number $op $i="
echo "scale=2; $number $op $i " | bc
done
}
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"
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
Sorry about bits and snippit of information
So I am writing an average shell script program
so if use inputs
echo 1 3, .... | sh get_number
I would have to pull the numbers seperated by spaces from echo to be
var1 = 1, var2= 3, etc.
I tried
#!/bin/sh
sum=0
for i in $*
do
sum=`expr $sum + $i`
done
avg=`expr $sum / $n`
echo Average=$avg
but doesnt work....
do I include a read here?
also how would I do
sh get_number <file1>, <file2>... to grab numbers in them and sum them
in shell script?
Thanks
Sounds like you are looking for the read shell builtin:
% echo "1 2 3 4" | read a b stuff
% echo $b
2
% echo $stuff
3 4
To fix up your code:
for i in $*; do
sum=$(( sum + i ))
n=$(( n + 1 ))
done
echo "Average=$(( sum / n ))"
#!/bin/sh
while [ $# -gt 0 ]; do
(( i++ ))
(( sum += $1 ))
shift
done
echo "Average=$(( sum/i ))"
Note: This fails in dash which is the closest shell I could find to a real sh.
An example of reading values from files passed as command line arguments or from lines read from stdin:
add_to_sum() {
set $*
while [ $# -gt 0 ]; do
I=`expr $I + 1`
SUM=`expr $SUM + $1`
shift
done
}
I=0
SUM=0
if [ $# -gt 0 ]; then
# process any arguments on the command line
while [ $# -gt 0 ]; do
FILE=$1
shift
while read LINE; do
add_to_sum "$LINE"
done < "$FILE"
done
else
# if no arguments on the command line, read from stdin
while read LINE; do
add_to_sum "$LINE"
done
fi
# be sure not to divide by zero
[ $I -gt 0 ] && echo Average=`expr $SUM / $I`