Skip to content

Instantly share code, notes, and snippets.

@dekokun
Created January 19, 2012 13:40
Show Gist options
  • Save dekokun/1640094 to your computer and use it in GitHub Desktop.
Save dekokun/1640094 to your computer and use it in GitHub Desktop.
シェルスクリプトで再帰を用いて階乗計算
expr $1 + 1 >/dev/null 2>&1
if [ $? -ne 1 -a $? -ne 0 ]; then
echo "must be numeric"
exit
elif [ $1 -lt 0 ]; then
echo "must be more than 0"
exit
elif [ $1 -eq 1 -o $1 -eq 0 ]; then
echo 1;
else
echo $(expr $1 \* $($0 $(expr $1 - 1)))
fi
@dekokun
Copy link
Author

dekokun commented Jan 19, 2012

ex)
./recursive.sh 6
→ 720

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment