shell script创建库
先创建名称为 myfuns
# my script functions function addem { echo $[ $1 + $2 ] } function multem { echo $[ $1 * $3 ] } function divem { if [ $2 -ne 0 ] then echo $[ $1 / $2 ] else echo -1 fi }
然后创建脚本,名称为: test14.sh
#!/bin/bash # using functions defined in a library file ./myfuns value1=10; value2=5 result1=`addem $value1 $value2` result2=`multem $value1 $value2` result3=`divem $value1 $value2` echo "The result of adding them is: $result1" echo "The result of multiplying th is: $result2" echo "The result of dividing them is: $result3"
其中 ./myfuns 是调用该文件,具体使用时可能因路径不同而使用不同的路径,本文中两个文件放在同一目录下
运行 sh test14.sh
输出:
test14.sh: line 7: addem: command not found test14.sh: line 8: multem: command not found test14.sh: line 9: divem: command not found The result of adding them is: The result of multiplying th is: The result of dividing them is:
暂时还没找到错误在哪。