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;
}