- 创建
caculator.sh脚本,内容如下:
#!/bin/sh
usage="Usage: `basename $0` (add|sub|mul|div|all) parameter1 parameter2"
command=$1
first=$2
second=$3
function add(){
ans=$(($first + $second))
echo $ans
}
function sub(){
ans=$(($first - $second))
echo $ans
}
function mul(){
ans=$(($first * $second))
echo $ans
}
function div(){
ans=$(($first / $second))
echo $ans
}
case $command in
(add)
add
;;
(sub)
sub
;;
(mul)
mul
;;
(div)
div
;;
(all)
add
sub
mul
div
;;
(*)
echo "Error command"
echo "$usage"
;;
esac
- 修改执行权限
chmod 777 caculator.sh
- 测试
$ ./caculator.sh add 2 3
5
$ ./caculator.sh sub 2 3
-1