shell脚本不可执行远程服务器


#!/bin/bash
if [ -n "$1" ]; then
file= ls /etc/apache2/sites-available | grep $1
if [ $file ="true" ]; then
cd /etc/apache2/sites-available
path= grep DocumentRoot $1 | awk '{gsub(""","",$2); print $2}'
print $path
else
echo "not in server"
fi
fi
enter code here

当它运行到本地或服务器时,它们给出完美的输出,但当shell脚本运行到远程服务器时,它不运行

if [ -n "$1" ]; then
for HOST in ${HOSTS_WEBMODULE_DEV}; do
ssh root@$HOST bash -c "
cd /etc/apache2/sites-available
path= grep DocumentRoot $1 | awk '{gsub(""","",$2); print $2}'
"
done
当脚本运行到远程服务器时,错误是
bash: -c: option requires an argument
awk: cmd. line:1: {gsub(",,); print }
awk: cmd. line:1:       ^ unterminated string
awk: cmd. line:1: {gsub(",,); print }
awk: cmd. line:1:       ^ syntax error
bash: -c: line 3: syntax error near unexpected token `fi'
bash: -c: line 3: `     fi'

让bash为您省去正确转义的麻烦:

#!/bin/bash
# The function you want to run on the remote side
myfunc() {
cd /etc/apache2/sites-available
# Correctly assign the variable
path=$(grep DocumentRoot "$1" | awk '{gsub(""","",$2); print $2}')
# Correctly print the variable
echo "$path"
}
# Copy the function definition to the remote, then invoke it with
# the script's $1, correctly escaped.
ssh "root@$HOST" "$(declare -f myfunc); myfunc ${1@Q}"

最新更新