如何在命令参数中调用 Bash 函数?
有这样一个目录系统:
Aircrack-NG_scripts
|
+--+-system
| |
| +----Wifi_get-interface.sh
|
+----Wifi_Normal-Mode.sh
系统目录下的 Wifi_get-interface.sh 文件包含以下代码:
#!/bin/bash
function get_interface {
read -p 'Wifi Interface: ' interface
return $interface
}
function get_mon_interface {
get_interface
mon_interface="${interface}mon"
return $mon_interface
}
export -f get_interface
export -f get_mon_interface
在 Wifi_Normal-Mode.sh 文件中是这样的:
#!/bin/bash
source ./system/Wifi_get-interface.sh
./system/Wifi_get-interface.sh
airmon-ng stop get_mon_interface
service network-manager start
当我执行 Bash 脚本 Wifi_Normal-Mode.sh Aircrack-NG 说get_mon_interface找不到接口。我需要在执行 Bash 脚本 Wifi_Normal-Mode.sh 时,它会从系统目录中的 Wifi_get-interface.sh 文件中获取函数,并在 sudo airmon-ng stop get_mon_interface 命令中执行此函数。
怎么修?
这里有两个问题 - 必须在 call 的上下文中调用
$()函数,并且函数本身只能返回退出代码(即整数),因此必须打印该行。像这样的东西:在此示例
"$(foo)"中将替换为结果echo "${interface}"