Solved

Bourne shell programming

Posted on 2002-07-24
5
360 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 117
unable to delete all specified values regedit 38 544
dog bark java program 15 109
twoTwo  challenge 35 107
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…
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.

839 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