?
Solved

Bourne shell programming

Posted on 2002-07-24
5
Medium Priority
?
364 Views
Last Modified: 2013-12-26
I'm writing a Bourne shell script. It calculates a factorial of an integer (n).

#!/bin/sh
echo "n = \c"
read n
total = 1
i     = 1
while [ "$i" -le "$n" ]
do
  total = `expr $total * $i`
  i     = `expr $i + 1`
done
echo "$n factorial = $total"

When I ran it, I had the following errors:

i not found
total not found

Could somebody point out the errors in the script for me ?

Thanks.
0
Comment
Question by:kevinnguyen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 260 total points
ID: 7175523
#!/bin/sh
echo "n = \c"
read n
total=1
i=1
while [ "$i" -le "$n" ]
do
    total=`expr $total \* $i`
    i=`expr $i + 1`
done
echo "$n factorial = $total"
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7175788
previous suggestion in words: remove blanks arround =
0
 

Expert Comment

by:rajiv_indya
ID: 7176358
#!/bin/sh
echo "n = \c"
read n
total=1
i=1
echo $total
echo $i
while [ "$i" -le "$n" ]
do
 total=`expr $total \* $i`
 i=`expr $i \+ 1`
done
echo "$n factorial = $total"



This I have tested is working
Problem in Shell Scripting spaces does matter , donot give unnecessary spaces or tabs in between . Also * is the special character having special meaning of all files so use \ along with it to hide its special meaning.

I think this is what u wanted
0
 
LVL 21

Expert Comment

by:tfewster
ID: 8020809
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Answered by ozo

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8066963
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question