Results 1 to 2 of 2

Thread: Help

  1. #1
    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"

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,004
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •