![如何在 Ubuntu 中使用链式函数](https://linux22.com/image/875911/%E5%A6%82%E4%BD%95%E5%9C%A8%20Ubuntu%20%E4%B8%AD%E4%BD%BF%E7%94%A8%E9%93%BE%E5%BC%8F%E5%87%BD%E6%95%B0.png)
我正在尝试执行以下脚本,但无法弄清楚如何将我从另一个函数的参数中得到的值作为参数传递。由于我是 Bash 脚本的初学者,因此带解释的答案将更加受欢迎。
function execute{
task = $1
servername = $2
$task $servername
}
function installDependency{
servername = $1
sshpass -p password ssh servername 'bash -s' < install_dependency.sh
}
function removeDependency{
servername = $1
sshpass -p password ssh servername 'bash -s' < remove_dependency.sh
}
execute remove_dependency server1
我收到以下错误:
syntax error near unexpected token `task=$1'
答案1
避免在声明中使用空格字符
servername="$1"
代替
servername = $1
function removeDependency {
servername="$1"
sshpass -p password ssh servername 'bash -s' < remove_dependency.sh
}
function execute {
task="$1"
servername="$2"
"$task" "$servername"
}
function installDependency {
servername="$1"
sshpass -p password ssh servername 'bash -s' < install_dependency.sh
}
execute "removeDependency" "server1"