我正在尝试执行以下脚本,但无法弄清楚如何将我从另一个函数的参数中得到的值作为参数传递。由于我是 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"