If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Help

LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Registered User
Join Date: Nov 2012
Posts: 1

Hello Guys,For my homework I must write a shell script to do this serie,

I know that I must use the "bc" for that, but for the script's itself i have no idea,(beginner)

Can you plz just help me for have some idea?

I wrote a script but it did not worked

echo "Input number"
read n
while [ $counter -ne $n ]
ans=`expr 4*(-1/(2*$counter+1))`| bc -l
counter=`expr counter+1`

echo "the sum of the series is :-$ans"
Reply With Quote
  #2 (permalink)  
Programming since 1BC
Join Date: Sep 2009
Location: Ontario
Posts: 994
First. The input to bc should be an expression, not a computed value.
ans=`echo 1 + 3 |bc`
Second. The 'expr' command requires the variables and operators to be separated by space.
count=`expr $count + 1`
You initialize ans to 1, but change it anyway before it is used, and at the end, you print it with a literal that suggests that you have summed the value of each expression calculated, but nowhere do you accumulate the answer.
You need to add
total=`expr $total + $ans`
and change the last line to echo total instead of ans.
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On