The program takes input but show the following errors, how to resolve those errors ?
CodePudding user response:
[
is not mere syntax, it's a command, therefore it needs space to separate it from its arguments.
while [ $counter -le $number ]
# .....^....................^
Make sure you validate that number
is actually only digits.
Some other comments:
- Use
$(...)
instead of`...`
-- see https://github.com/koalaman/shellcheck/wiki/SC2006 for more details. - bash can do arithmetic, you don't need to call out to
expr
. See Arithmetic Expansion and Shell Arithmetic in the manual. There is also an arithmetic conditional construct (analogous to the string-oriented[[...]]
conditional construct) -- see Conditional Constructs and scroll down to((...))
(there's no direct link).
CodePudding user response:
I think you need to add a space after [
and before ]
:
#!/bin/bash
echo "Enter the number to find it's factorial"
read number
total=1
counter=1
while [ $counter -le $number ];
do
total=` expr $counter \* $total`
counter=` expr $counter 1`
done
echo $total
Working here on Ubuntu:
$ ./factorial.sh
Enter the number to find it's factorial
5
120
CodePudding user response:
Actually, on line 7, you forgot the completing backtick.