Home > Net >  Bash script to find factorial
Bash script to find factorial

Time:11-16

The program takes input but show the following errors, how to resolve those errors ? enter image description here

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:

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.

  • Related