Solved

Bourne shell programming

Posted on 2002-07-24
5
356 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
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 65 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 20

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now