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

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

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?
Thanks


I wrote a script but it did not worked

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

echo "the sum of the series is :-$ans"
Reply With Quote
  #2 (permalink)  
Old
Programming since 1BC
 
Join Date: Sep 2009
Location: Ontario
Posts: 982
First. The input to bc should be an expression, not a computed value.
Code:
ans=`echo 1 + 3 |bc`
Second. The 'expr' command requires the variables and operators to be separated by space.
Code:
count=`expr $count + 1`
Three,
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
Code:
total=`expr $total + $ans`
and change the last line to echo total instead of ans.
Reply With Quote
Reply

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