深入探索Shell脚本函数与图形化脚本编写
1. 脚本中使用函数库
在Shell脚本里,我们能够借助函数库来提升代码的复用性。例如下面这个脚本 test14 :
#!/bin/bash
# using functions defined in a library file
. ./myfuncs
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 them is: $result2"
echo "The result of dividing them is: $result3"
运行该脚本后,输出如下:
The result of adding them is: 15
The result of multiplying them is: 50
The result of dividing them is: 2
此脚本成功运用了 myfuncs 库文件里定义的函数。
2. 在命令行使用函数
脚本函数可用于构建复杂操作,并且能直接在命令行界面使用。定义函数的方式有以下两种:
-
超级会员免费看
订阅专栏 解锁全文
3423

被折叠的 条评论
为什么被折叠?



