提升脚本效率:函数创建与图形化脚本编写指南
1. 使用函数库
使用函数库的关键是 source 命令,它能在当前 shell 上下文中执行命令,而非创建新的 shell 来执行。通过 source 命令,可在 shell 脚本中运行库文件脚本,使脚本能够使用库中的函数。
source 命令有个快捷别名,即点运算符。若要在 shell 脚本中引入 myfuncs 库文件,只需添加以下代码:
. ./myfuncs
此示例假定 myfuncs 库文件与 shell 脚本位于同一目录。若不在同一目录,则需使用合适的路径来访问该文件。以下是一个使用 myfuncs 库文件的脚本示例:
$ cat 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:
超级会员免费看
订阅专栏 解锁全文
1350

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



