如何执行所有使用此处文档的脚本参数?
How can I execute all script parameters that use here doc?
我有一个简单的脚本,我在我的代码库中使用它,称为 sshpass
。
#!/bin/bash
expect << EOF
spawn $@
expect {
"*assword" {
send "$SSHPASS\n"
}
}
expect eof
EOF
我目前使用这个脚本的方式是这样的-
./sshpass scp archive.tgz $SERVER:$DIR
当 SSH 命令是单行命令时,这非常有效。我的问题是当我尝试通过使用此处文档的 sshpass
执行命令时。
./sshpass ssh $user@$server /bin/bash << EOF
echo "do this..."
echo "do that..."
echo "and the other..."
EOF
以上失败,因为 $@
只解析出 ssh $user@$server /bin/bash
.
请不要评论我如何处理我的 SSH 身份验证。当被迫使用 Cygwin 时,有一些特定的事情,例如密钥身份验证和管理权限,根本不起作用。
heredoc 将替换您脚本的标准输入。如果您希望它作为参数访问,请使用命令替换,如
./sshpass ssh $user@$server /bin/bash $(cat << EOF
echo "do this..."
echo "do that..."
echo "and the other..."
EOF
)
虽然这可能不会完全按照您的意愿结束,因为它会将每个单词作为它自己的位置参数传递,所以您将 运行
ssh $user@$server echo "do this..." echo "do that..." echo "and the other..."
这将使第一个 echo 得到所有其余的作为参数。您需要在每个命令的末尾使用分号,并在整个命令周围加上引号,这样您就不会在远程执行某些命令而在本地执行某些命令。所以我应该推荐它为:
./sshpass ssh $user@$server /bin/bash "$(cat << EOF
echo 'do this...';
echo 'do that...';
echo 'and the other...'
EOF
)"
但这仍然给我一种不安的感觉,因为很可能很容易 "do the wrong thing" 用这样的东西
我有一个简单的脚本,我在我的代码库中使用它,称为 sshpass
。
#!/bin/bash
expect << EOF
spawn $@
expect {
"*assword" {
send "$SSHPASS\n"
}
}
expect eof
EOF
我目前使用这个脚本的方式是这样的-
./sshpass scp archive.tgz $SERVER:$DIR
当 SSH 命令是单行命令时,这非常有效。我的问题是当我尝试通过使用此处文档的 sshpass
执行命令时。
./sshpass ssh $user@$server /bin/bash << EOF
echo "do this..."
echo "do that..."
echo "and the other..."
EOF
以上失败,因为 $@
只解析出 ssh $user@$server /bin/bash
.
请不要评论我如何处理我的 SSH 身份验证。当被迫使用 Cygwin 时,有一些特定的事情,例如密钥身份验证和管理权限,根本不起作用。
heredoc 将替换您脚本的标准输入。如果您希望它作为参数访问,请使用命令替换,如
./sshpass ssh $user@$server /bin/bash $(cat << EOF
echo "do this..."
echo "do that..."
echo "and the other..."
EOF
)
虽然这可能不会完全按照您的意愿结束,因为它会将每个单词作为它自己的位置参数传递,所以您将 运行
ssh $user@$server echo "do this..." echo "do that..." echo "and the other..."
这将使第一个 echo 得到所有其余的作为参数。您需要在每个命令的末尾使用分号,并在整个命令周围加上引号,这样您就不会在远程执行某些命令而在本地执行某些命令。所以我应该推荐它为:
./sshpass ssh $user@$server /bin/bash "$(cat << EOF
echo 'do this...';
echo 'do that...';
echo 'and the other...'
EOF
)"
但这仍然给我一种不安的感觉,因为很可能很容易 "do the wrong thing" 用这样的东西