Hi I'm writing a shell script and I'm trying to get a starting number and an ending number from the user.
So if the users starting number is 3 and the ending number is 4, my program should print out 4 5 6 7 as the consecutive numbers. My problem however is that my program is printing "
./unixhw1.sh: line 20: [: too many arguments" over and over instead. How can I go about fixing it?
"

The shell script I'm working on

 #!/bin/bash
    echo "type the starting number"
    read num1

    echo "type how many consecutive numbers."
    read num2

    sum=`expr $num1 + $num2;`
    newnum=`expr $num1`;
    oddtotal=0;
    n=0;

    until [ "$n" -gt "$num2" ]; do
         if [ "$newnum" -lt "$sum" ] ; then
         newnum=$((newnum+1))
         echo "$newnum"
         n=$((n+1))
         fi

        if [ "$newnum" %2 -ne 0 ] ; then
        oddtotal=oddtotal+newnum;
        fi
    done

    echo "$num1 is the first number, the next $num2 cosective numbers are and so the
     sum of these odd numbers are $oddtotal"

My code using C++

 #include <iostream>
    using namespace std;

    int main()
    {
        int num, consecnum, sum, i, num3, j, oddtotal;
        oddtotal = 0; 

        cout<<"Enter a starting number"<<endl;
        cin>>num;

        cout<<"Enter a consecutive number."<<endl;
        cin>>consecnum;


        sum = num + consecnum; 
        num3 = num;

        cout<<" "<<endl; 

        for(i=0; num3<sum; i++) 
        {
             num3+=1;
             cout<<num3<<endl; 


        if(num3 % 2 != 0)
        {
             oddtotal+=num3;
        }

        }

        cout<<" "<<endl;
        cout<<num<<" is the first number, the next "<<consecnum<<" consecutive numbers are "<< " "<<"sum of odd nums "<<oddtotal<<endl;


        system("pause");
        return 0;


}

Recommended Answers

All 9 Replies

hi,

inside this test

if [ "$newnum" %2 -ne 0 ] ; then

$newnum %2 needs to be 'arithmetically' evaluated.

also, numbers don't need to be quoted.

so i have to use != instead of ne?

no!
use: if test $((newnum %2)) -ne 0; then...

or non-POSIX: if ((newnum %2)); then...
because, inside bash's arithmetic evaluation, zero returns an error;
so,
if evaluation is not zero, then true

NB: = and != are for lexical tests.

Ah ok, but why does "test" need to be added after "if". Also, the solution you gave works great but somehow my last echo statement isn't printing out.

because the until loop never ends, as $n is never greater than $num2, because $newnum is never equals to $sum.

#!/bin/bash

read -p 'type the starting number: ' num1

read -p 'type how many consecutive numbers: ' num2

for ((n=num1;n<=num2;n++))
do
   ((n%2?oddtotal+=n:0))
done

echo "$num1 is the first number, the next $num2 consecutive numbers are and so the
sum of these odd numbers are $oddtotal"

Sorry for the double post but if I wanted to the get the starting number and consecutive number using command line parameters would this be the way to do it?

#!/bin/bash
# sh unixhw1 1 2

echo "type the starting number $1"

echo "type how many consecutive numbers $2"

sum=`$1 + $2;`
newnum=`$1`;
oddtotal=0;
n=0;

until [ "$n" -gt "$2" ]; do
     if [ "$newnum" -lt "$sum" ] ; then
     newnum=$((newnum+1))
     echo "$newnum"
     let n+=1
     fi

     if test $((newnum % 2)) -ne 0; then
     oddtotal=oddtotal+newnum;
     fi
done

echo "$1 is the first number, the next $num2 cosective numbers are and so the su
m of these odd numbers are $oddtotal"

exit 0

sum=$1 + $2;

wrong: command substitution (backticks) doesn't evaluate arithmetics.
btw, expr is not part of shell, and is useless as shells are able to do arithmetic evaluation.

This code works except that "3" prints for the oddtotal when I enter 3 and 4 for num1 and num2 respectively. Something isn't right somewhere.

#!/bin/bash
read -p 'type the starting number: ' num1
read -p 'type how many consecutive numbers: ' num2
for ((n=num1;n<=num2;n++))
do
   ((n%2?oddtotal+=n:0))
done
echo "$num1 is the first number, the next $num2 consecutive numbers are and so t
he
sum of these odd numbers are $oddtotal"

myCode:

#!/bin/bash

read -p 'type the starting number: ' num1

read -p 'type how many consecutive numbers: ' num2

for (( n=num1; n<=num1+num2; n++))
do
   test $n -eq $num1 || { ar+=( $n ); ((n%2?oddtotal+=n:0));}
done
echo "the first number is $num1"
echo "the next $num2 consective numbers are ${ar[@]}"
echo "the sum of the odd numbers of this list is $oddtotal"

execution:

./myCode
type the starting number: 3
type how many consecutive numbers: 4
the first number is 3
the next 4 consective numbers are 4 5 6 7
the sum of the odd numbers of this list is 12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.