Solved

Bourne shell programming

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 96
mixString challenge 36 134
repeatEnd java challenge 42 85
How to know only "File created" with EventLog 3 35
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…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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