Skip to content

Instantly share code, notes, and snippets.

@technoburst
Last active December 15, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technoburst/5298360 to your computer and use it in GitHub Desktop.
Save technoburst/5298360 to your computer and use it in GitHub Desktop.
Shell script to find the factorial of a number
#!/bin/bash
#command to find the factorial of a number
#Syntax : fact [arg]
declare -i count result
# The script will treat subsequent occurrences of "count", "result" as an integer.
if test -z $1 ; then
echo "Error : Argument missing"
exit 1
else
tempvar=$(expr $1 + $1)
if test -z $tempvar ; then
echo "Error : Non integer argument"
exit 1
fi
fi
count=$1
result=1
while [ "$count" != "0" ]; do
result=result*count
count=count-1
done
echo "$result"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment