[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Bourne shell programming

Posted on 2002-07-24
5
Medium Priority
?
366 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 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