This question already has answers here:
Bash Script - Fibonacci
(6 answers)
bash shell script fibonacci not showing value 2 after 0 1 1?
(2 answers)
Closed 3 years ago.
Is it possible to write a script that calculates the nth Fibonacci number - iteratively. I did it recursive (showed below) but could find solution iteratively. Please help.
#!/bin/bash
fib()
{
if [ $1 -le 0 ]
then
echo 0
return 0
fi
if [ $1 -le 2 ]
then
echo 1
else
a=$(fib $[$1-1])
b=$(fib $[$1-2])
echo $(($a+$b))
fi
}
I'm not into bash and had no possibility to check but it should be something like:
fib()
{
if [ $1 -le 0 ]
then
echo 0
return 0
fi
if [ $1 -le 2 ]
then
echo 1
else
a = 1
b = 1
for i in {2..$1}
do
c = $b;
b = $a + $b;
a = $c
done
echo $($b)
fi
}
(there might be some errors in spelling)
for java it would work like:
public int fib(n){
if(n <= 1) {
return n;
}
int fib = 1;
int prevFib = 1;
for(int i=2; i<n; i++) {
int temp = fib;
fib+= prevFib;
prevFib = temp;
}
return fib;
}
Here's the iterative approach:
function fib() {
local n=$1
local x=0
local prev1=0
local prev2=0
local cur=0
for (( x = 1 ; x <= n ; x++ ))
do
if [[ $x == 1 || $x == 2 ]] ; then
prev1=1
prev2=1
cur=1
continue
fi
cur=$(( prev1 + prev2 ))
prev2=$prev1
prev1=$cur
done
echo $cur
}
Related
what should be condition for while before adding while loops it prints even no upto 100 but I need to print sum of even numbers
#!/bin/bash
sum=0
for((n=2;n<=100;n=n+2))
do
echo $n
while [[$n 0]] # what should be condition for while loop
do
sum= `expr sum + $n`
done
echo "sum is $sum "
done
Does this count ^_* :
kent$ seq -s + 2 2 100|bc
2550
k=0;
for i in {1..100}; do
if [[ $(( i % 2 )) == 0 ]]; then
let k=k+i
fi ; done
echo $k
Prints
2550
or this:
k=0; for i in {1..100}; do
if (( i % 2 == 0 )); then
(( k=k+i )) ; fi ; done ; echo $k
You shouldn't have the while loop at all. You're already iterating with the for loop.
#!/bin/bash
sum=0
for((n=2;n<=100;n=n+2))
do
echo $n
((sum+=n))
done
echo "sum is $sum "
I'm trying to write a bash script for
"Given a positive integer N greater than 1, make a script to show the
even numbers between 0 and N. Ex .: The number 12 has been read. The
program must have as output: 0, 2, 4, 6, 8, 10 and 12;"
So far I did like this:
ex5.sh
#!/bin/bash
echo "Number :"
read num;
for (( i = 1; i >= $num; i++ ))
do
if [ $(($i % 2)) -eq 0 ]
then
echo $i
fi
done
When i compile, it doesn't print numbers. I couldn't find where is the problem
I tried using for-in loop as well
#!/bin/bash
echo "Number :"
read num;
for i in $num
do
if [ $(($i % 2)) -eq 0 ]
then
echo $i
fi
done
This time only print the number that i put as a input. For example, I put 4 in terminal it prints 4 as well
Many thanks
This way can make your code working:
echo -n "Number : "
read num;
for i in $(seq 0 $num)
do
if [ $(expr $i % 2) == 0 ]
then
echo $i
fi
done
bash ex5.sh
Number : 12
0
2
4
6
8
10
12
`
echo -n "Number : "
read num;
for i in $(seq 0 $num)
do
if [ $((num%2)) -eq 0 ]
then
echo $i
fi
done
`
#!/bin/bash
read -p "Number: " num
awk '{for(i=0;i<=$1;i++) if(i%2 == 0) print i}'<<<$num
./printNumbers.sh
Number: 8
0
2
4
6
8
# to get a list use printf
awk '{for(i=0;i<=$1;i++) if(i%2 == 0) printf i","}'<<<$num | sed 's/,$//'
./printNumbers.sh
Number: 8
0,2,4,6,8
#!/usr/bin/env bash
read -p 'Number: ' num
seq 0 2 "$num"
it's <= not >=
#!/bin/bash
echo "Number :"
read num;
for (( i = 1; i <= $num; i++ ))
do
if [ $(($i % 2)) -eq 0 ]
then
echo $i
fi
done
I'm getting an error saying
"line 6: [: : integer expression expected"
and I can't figure out what to do to fix it. I'm trying to write a script to print out 200 equations, the equations should be of form "i * j = k" where i is an integer between 1 and 10, j is between 1 and 20, k is the product of i and j.
#!/bin/bash
for i in {1..200..1}
do
if [ "$i" -gt 0 ] && [ "$i" -lt 11 ] && [ "$j" -gt 0 ] && [ "$j" -lt 21 ]
then
i = 1
j = 1
k = $(($i * $j))
echo $i * $j = $k
((i++))
((j++))
fi
done
In your script, both i and j get initialized to 1, which means your entire loop echoes 1 * 1 = 1, 200 times. Furthermore, j is not defined the first time your if statement tests $j, hence you got the error message "line 6: [: : integer expression expected".
One way of printing 200 equations, with combinations of i and j, where i is an int between 1 and 10, and j is between 1 and 20, is as follows:
#!/bin/bash
for (( i = 1; i <= 10; i++ )); do
for (( j = 1; j <= 20; j++ )); do
k=$(( i * j )) # Note no space before/after equal sign
echo "$i * $j = $k" # Note the quotation mark
done
done
Or you can do the same thing in a different format as follows:
#!/bin/bash
for i in {1..10}; do
for j in {1..20}; do
k=$(( i * j ))
echo "$i * $j = $k"
done
done
This way, both i and j get initialized before any statements are executed, and you can set the max and min restrictions on both within the loops.
I've defined this function (with some debugging printouts):
listcontains() {
for item in $1
do
echo $2 = $item ?
if [ "$2" == "$item" ]; then
echo YES!
return 1;
fi
done
echo NO!
return 0
}
And I'm using it like this:
list="1 2"
if listcontains "$list" 3; then echo Y; else echo N; fi
I'm expecting N, but the result is Y, and the debug output makes it even more strange:
3 = 1 ?
3 = 2 ?
NO!
Y
What did I do wrong?
In shell, you use 0 to denote true (and some positive number, i.e. 1, to denote false). It's the same notion as for scripts exit values - in unix world, 0 means success.
So to fix, just reverse return values:
listcontains() {
for item in $1
do
echo $2 = $item ?
if [ "$2" == "$item" ]; then
echo YES!
return 0;
fi
done
echo NO!
return 1
}
Example:
$ list="1 2"
$ if listcontains "$list" 3; then echo Y; else echo N; fi
3 = 1 ?
3 = 2 ?
NO!
N
$ list="1 3"
$ if listcontains "$list" 3; then echo Y; else echo N; fi
3 = 1 ?
3 = 3 ?
YES!
Y
I am trying to perform bubble sort is Unix shell script. Why is my code not working?
a=(10 8 20 25 12)
for ((i=0;i<5;i++))
do
for((j=0;j<5;j++))
do
if ((${a[j]} > ${a[$((j+1))]}))
then
v=${a[$j]}
a[$j]=${a[$((j+1))]}
a[$((j+1))]=$v
fi
done
done
echo ${a[*]}
echo "end..."
I guess this is homework. therefore I don't give codes, just point out the errors in your codes:
for((j=0;j<5;j++)) then read a[j+1], here would be problem because when j=4, j+1 doesn't exist
fix that, your program will sort.
Try this:
echo "Enter size of array";
read n; #get the size of array from user.
echo "Enter the array";
read -a arr; #get the array form user eg: 2 3 4 5 6
echo "Orignal array is: ${arr[*]}"; #print orignal array
flag=1;
for (( i = 0; i < $n-1; i++ ))
do
flag=0;
for ((j = 0; j < $n-1-$i; j++ ))
do
if [[ ${arr[$j]} -gt ${arr[$j+1]} ]]
then
temp=${arr[$j]};
arr[$j]=${arr[$j+1]};
arr[$j+1]=$temp;
flag=1;
fi
done
if [[ $flag -eq 0 ]]; then
break;
fi
done
echo "Final sorted Array is ${arr[*]}";