function fname() { 程式段 }
[dywang@dywmac zzz]$ vim sh15.sh [dywang@dywmac zzz]$ cat sh15.sh #!/bin/bash factorial(){ if [ "$1" -gt "1" ]; then i=$(($1 - 1)) j=$(factorial $i) k=$(($1 * j)) echo $k else echo 1 fi } num=$(echo $@ | egrep -o "[0-9]+") for i in $num; do result=$(factorial $i) echo "$i!=$result" done
[dywang@dywmac zzz]$ sh sh15.sh 23nmdf4sd5 7u8 23!=8128291617894825984 4!=24 5!=120 7!=5040 8!=40320
[dywang@dywmac zzz]$ cat sh15.sh #!/bin/bash num=$(echo $@ | egrep -o "[0-9]+") for i in $num; do result=$(factorial $i) echo "$i!=$result" done factorial(){ if [ "$1" -gt "1" ]; then i=$(($1 - 1)) j=$(factorial $i) k=$(($1 * j)) echo $k else echo 1 fi }
[dywang@dywmac zzz]$ sh sh15.sh 23nmdf sh15.sh: line 5: factorial: command not found 23!=