i write one linux code for Fibonacci series(0 1 1 2 3 5 8) but when i run its always showing me else statement.
#!/bin/bash
#This program will show series of Fibonacci numbers upto user input.
echo -n "Enter the number for Fibonacci series: "
read num
if [ $# = 1 ]
then
x1 = 0
x2 = 1
echo "The Fibonacci series for the number $num is: "
for (( y=0;$y<num; y=$y+1 ))
do
echo -n "$x1 "
x2 = $(( $x2 + $1 ))
x1 = $(( $x2 - $x1 ))
done
else
echo "Input is wrong"
fi
if i remove if satement then i got error of line 10 and 11 and of for loop
I'm not sure why you're checking the number of arguments, but you either need to provide one argument or change it to check for zero arguments. Also you have a few spaces that will cause problems. You'll need to remove those. Lastly, as #bnaecker mentioned, you'll want to use -eq instead of = when comparing numerical equality.
#!/bin/bash
#This program will show series of Fibonacci numbers upto user input.
echo -n "Enter the number for Fibonacci series: "
read num
if [ $# = 0 ]
then
x1=0
x2=1
echo "The Fibonacci series for the number $num is: "
for (( y=0;$y<num; y=$y+1 ))
do
echo -n "$x1 "
x2=$(($x2 + $1))
x1=$(($x2 - $x1))
done
else
echo "Input is wrong"
fi
Related
I have to write a script in bash where I have to provide 10 numbers to the table. Then script have to write the content out and arithmetic mean of even numbers. I did like 90% of the script, but I can't find out how to extract information about quantity of even numbers that is needed for arithmetic mean.
Here is my code:
echo "Provide data:"
i=0
for (( i = 0 ; i < 10; i++ ))
do
echo "Provide $[$i+1] number:"
read x
if [ "$x" = "" ]
then
break
else
table[$i]=$x
fi
done
echo "Provided data: ${table[*]}"
result=0
for (( i = 0 ; i < 10; i++))
do
res=$[${table[i]}%2]
if [ $res -eq 0 ]
then
echo "Number ${table[i]} is even"
result=$[$result+${table[$i]}]
fi
done
echo "SUM:$[$result]"
ignoring data input adding only odd inputs can look like :
$ cat c.sh
#!/bin/bash
declare -A xDarray
sum=0
xDarray[0 1]=1
xDarray[0 2]=3
xDarray[1 0]=2
xDarray[2 0]=4
for var in ${xDarray[#]}
do
if [ $(( $var & 1 )) == 0 ] ; then
echo $var is even
i+=1
tab[$i]=$var
sum=$(( $sum + $var))
fi
done
var=$(echo ${tab[#]} | sed 's/ / + /g' )
echo $var = $sum
result in
$ ./c.sh
2 is even
4 is even
2 + 4 = 6
$
whatever the number of data is used it would work
I let you work around your data input
Here are some suggested modifications for your script, syntax is simple.
#!/bin/bash
arr=()
for (( i=1;i<=10;i++ )); do
number=''
while [[ ! $number =~ ^[0-9]+$ ]]; do
printf "Please enter number $i:\n"
read number
done
arr+=($number)
done
printf "\nProvided numbers:"
printf " %d" "${arr[#]}"
printf "\nEven numbers:"
s=0
n=0
for x in "${arr[#]}"; do
if ! (( x % 2 )); then
printf " %d" "$x"
s=$(( s + x ))
(( n++ ))
fi
done
m=$(( s / n ))
printf "\nMean of the %d even numbers: %d / %d = %d\n" "$n" "$s" "$n" "$m"
Use an array arr to hold the input numbers, declare with arr=(), append numbers with arr+=($x), we refer only ${arr[#]} for all the items and we avoid any other complex array references, indices etc.
Every input number is tested against regular expression ^[0-9]+$ which means one or more digits (and only digits) with the =~ operator, and if this is not true, we prompt again for the same i-th input number.
Also we prefer printf for printing.
The last loop is the standard array loop, where we use again the arithmetic expansion syntax to find the even numbers, to add them to the sum and get the mean of them (result is an integer).
If you want to print a decimal result, e.g. with 2 floating points, you could use bc and printf "%f" like this:
m=$( bc <<< "scale=2; $s/$n" )
printf "%.2f" "$m"
I'm trying to write a script that takes a list of integers as command line arguments, calculates the square of each integer, then gives me the sum of the squares. Here's what I have so far...
if [ $# = 0 ]
then
echo "Usage: $0 integer-list"
exit 1
fi
for list in "$#"
do
echo "The square of $list is: $(($list*$list))"
done
As you can see, I have a simple for loop to deal with the squares, I'm just not sure how to get the sum of those squares and echo that to the screen. Any suggestions?
Just add a sum of previous squares to current square of item from the list.
#!/bin/bash
if [ $# = 0 ]
then
echo "Usage: $0 integer-list"
exit 1
fi
SUM=0
for ITEM in "$#"
do
SUM=$(($SUM+$ITEM*$ITEM))
done
echo "Sum of squares is :" $SUM
On the command line -
$: for n in 2 3 4
> do q=$((n*n))
> echo "square of $n is $q"
> s=$((s+q))
> done; echo "Sum of squares: $s"
square of 2 is 4
square of 3 is 9
square of 4 is 16
Sum of squares: 29
In a script, you can just say
for n in "$#"
My Linux class just started to begin scripting and I am having trouble with one of my questions. The question is as follows: Use a script to take two numbers as arguments and output their sum using bc.
This is my first script. To get the script to execute you need to do chmod +x filename
This is what I have so far:
#!/bin/bash
read -p "Enter in a numeric value: " num1
read -p "Enter in a second numeric value: " num2
if [ $# -ne 2 ] ; then
echo "Enter in two numeric arguments"
else
echo "The sum of the entered values are: "
echo "$num1 + $num2"|bc
fi
I keep running into an error. When I enter in two values it displays "Enter in two numeric arguments". It shouldn't do that because if I enter in two values, the if statement will evaluate to false and go to the else statement. Is my logic wrong or am I approaching this the wrong way?
You can use a while loop and break.
n=0
ex=""
while [ 1 ]
do
read -p "Enter read in a numeric value" num
n=$(( n + 1))
if [[ $n -eq 2 ]]
then
ex=${ex}${num}
echo "Sum of the two values are"
echo ${ex} | bc
break
else
ex=$num" + "
fi
done
I am trying to write a script which will check number of arguments for first and second number; if both variable entered, it will do the calculation; if one argument is missing, it will print error message.
Here what I've done so far:
#!/bin/bash
echo -n "Enter the first number: "
read num1
echo -n "Enter the second number: "
read num2
if [ $# -le 1 ]; then
echo "Illegal number of arguments"
exit
else
echo "The sum is: " $(( num1 + num2 ))
fi
I am always getting error message even though I enter both of the numbers. What am I missing? Please help.
Test Your Assigned Variables, Not Positional Parameters
Your variables num1 and num2 aren't positional parameters, and so the special parameter $# is probably not what you think it is. You should change your conditional to check that both variables are set. For example:
declare -i num1 num2
read -p 'Enter the first number: ' num1
read -p 'Enter the second number: ' num2
if [ -z "$num1" ] || [ -z "$num2" ]; then
echo "Illegal number of arguments" >&2
else
echo "The sum is: $((num1 + num2))"
fi
Looks like you are messing up command line arguments and variables you are reading interactively. $# has nothing to do with variables you declared and/or read from command line. It is the number of command line arguments. You need to check the variables you attempted to read from console:
#!/bin/sh
echo -n "Enter the first number: "
read num1
echo -n "Enter the second number: "
read num2
[ -z $num1 ] || [ -z $num2 ] && echo "Illegal number of arguments" && exit 1
echo "The sum is: " $(( num1 + num2 ))
On the other hand, if you really want to check command line arguments, the script will be even simpler:
#!/bin/sh
[ -z $2 ] && echo "Illegal number of arguments" && exit 1
echo "The sum is: " $(( $1 + $2 ))
Here $1 refers to the first argument, $2 refers to the second argument and so on. $0 refers to the name of the script itself.
So, the way you have your program setup right now, it takes input through the read command, but that isn't the same as passing in an argument.
You pass an argument through the CLI, for instance:
./sum.sh 5 2 # => 7
Where sum.sh is the name of the file and 5 and 2 are your arguments.
So, the reason you keep getting "Illegal number of arguments" is because in bash the $# variable holds the number of arguments, but since you're reading the values in from the code, $# will always less than 1 because no arguments have been provided.
What I think you're looking for is something like this:
#!/bin/bash
if [ $# -le 1 ]; then
echo "Illegal number of arguments"
exit
else
echo "The sum is: " $(( $1 + $2 ))
fi
This article is pretty good if you want to learn more: http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html
I'm trying to take input from user and then based on that input either addition operation will be performed or subtraction will be performed. Choice 1 is for addition and 2 is for subtraction. The error I'm getting is this:
number.sh: line 12: d: command not found
(for addition line 12)
number.sh: line 17: d: command not found
(for subtraction line 17)
Here is the code:
#!/bin/bash
echo "enter choice"
echo "enter 1 for addition"
echo "enter 2 for subtraction"
read a
echo "entered choice is" $a
echo "now enter 2 numbers"
if [ $a = 1]; then
read b
read c
d = `expr $b + $c`
echo "addition of 2 numbers is" $d
elif [ $a = 2]; then
read b
read c
d = `expr $b - $c`
echo "subtraction of 2 numbers is" $d
else
echo "enter valid choice"
fi
You have extra spaces around your assignments and missing spaces around your [] expressions. Here's a corrected version. Note that when you use two read calls, you need a newline in between when giving the input. That is, you have to type 1 <Enter> 2 <Enter> 3 <Enter> on the command line to get a result of 5.
#!/bin/bash
echo "enter choice"
echo "enter 1 for addition"
echo "enter 2 for subtraction"
read a
echo "entered choice is" $a
echo "now enter 2 numbers"
if [ $a = 1 ]; then
read b
read c
d=`expr $b + $c`
echo "addition of 2 numbers is" $d
elif [ $a = 2 ]; then
read b
read c
d=`expr $b - $c`
echo "subtraction of 2 numbers is" $d
else
echo "enter valid choice"
fi
There should be no spaces around the = sign on line 12 and 17. Remove those spaces.
In bash, there should be no spaces around the assignment operator =.