上下文: 我有一个任务要在远程机器上运行一些脚本,然后退出
我有一个脚本,但我很困惑如何在其中使用换行符,
triggerPerformanceTest(){
report=$1
userDataFiles=$2
baseURL=$3
cdnURL=$4
streamingURL=$5
echo "Startin the Jmeter script"
ssh -tt -i Test.ppk username@<test server> <<EOF
cd apache-jmeter-3.1/bin/
JVM_ARGS="-Xms512m -Xmx25000m" ./jmeter.sh -n -t /home/ubuntu/JMeter/Test.jmx --jmeterproperty XMLReport=$report --jmeterproperty UserDataFile=$userDataFiles --jmeterproperty BaseUrl=$baseURL --jmeterproperty CdnUrl=$cdnURL --jmeterproperty StreamingUrl=$streamingURL --jmeterproperty isBenchMark=false --jmeterproperty Enable_DigitalExhaust=true --jmeterproperty Enable_Health=true --jmeterproperty HealthPollingInterval=6
exit
EOF
echo "Test successfully executed"
}
triggerPerformanceTest Log.csv UserDataFile.csv localhost localhost localhost
Run Code Online (Sandbox Code Playgroud)
在我运行 JMeter 脚本的第二步中,它有很多属性。有没有一种方法可以在此处使用换行符,以便将所有行视为一步。就像下面提到的
ssh user@server << EOF
command_one
command_two argument1 argument2 argument3 argument4
argument5 argument6 argument7
command_three
EOF
Run Code Online (Sandbox Code Playgroud)
在 here 文档中,<<(here EOF)之后的分隔符没有被引用,<backslash><newline>序列被删除,这是一个line continuation。
实际上,唯一<backslash><newline>没有被删除的情况是:
<backslash><backslash><newline>)
cat << EOF
foo\
bar
EOF
Run Code Online (Sandbox Code Playgroud)
产出
foobar
Run Code Online (Sandbox Code Playgroud)
所以,在这里你可以这样做:
ssh user@server << EOF
command_one
command_two argument1 argument2 argument3 argument4 \
argument5 argument6 argument7
command_three
EOF
Run Code Online (Sandbox Code Playgroud)
并且ssh最终会被喂饱:
command_one
command_two argument1 argument2 argument3 argument4 argument5 argument6 argument7
command_three
Run Code Online (Sandbox Code Playgroud)
在它的标准输入上。
即使您使用:ssh ... << 'EOF'为了避免在 here-document 内执行的参数扩展、命令替换和算术扩展,ssh也会提供:
command_one
command_two argument1 argument2 argument3 argument4 \
argument5 argument6 argument7
command_three
Run Code Online (Sandbox Code Playgroud)
但是远程 shell 会将其解释<backslash><newline>为行继续,因此会产生相同的效果。
请注意,当您执行以下操作时:
ssh user@server << EOF
Run Code Online (Sandbox Code Playgroud)
sshd在远程主机上运行用户的登录 shell 来解释该代码。因为它可以是任何东西,不一定是类似 Bourne 的 shell,所以最好运行:
ssh user@server sh << EOF
Run Code Online (Sandbox Code Playgroud)
Where sshdrunning user-s-login-shell -c sh,因此您知道 Bourne-like shell 正在解释您的代码。
例如,JVM_ARGS="-Xms512m -Xmx25000m" ./jmeter.sh...Bourne-shell 或兼容语法。它可以在csh, tcsh, rc, es, fishshell 中工作,因此ssh user@server sh << EOF如果useron的登录 shellserver是这些 shell 之一,则无法使用。
但一个显着的区别是,在这种情况下,user-s-login-shell它不是作为登录 shell 启动的,因此不会读取/etc/profile或~/.profile(或用户登录 shell 的等效项)来设置登录会话。
或者,您可以将该代码转换为与所有这些 shell 兼容的语法:(env JVM_ARGS='-Xms512m -Xmx25000m' ./jmeter.sh...使用单引号而不是双引号并用于env传递 env var 而不是 Bourne/rc 特定envvar=value cmd语法)。
可以使用xargs以下方法避免反斜杠:
ssh user@server sh << EOF
command_one
xargs command_two << END_OF_ARGS
argument1 argument2 argument3 argument4
argument5 argument6 argument7
END_OF_ARGS
command_three
EOF
Run Code Online (Sandbox Code Playgroud)
或通过使用像壳rc,ksh93,zsh,bash,yash和的数组:
使用rc/zsh语法:
ssh user@server zsh << 'EOF'
command_one
args=(
argument1 argument2 argument3 argument4
argument5 argument6 argument7
)
command_two $args
command_three
EOF
Run Code Online (Sandbox Code Playgroud)
(这里引用EOF以便$args不被本地shell扩展)。
或与ksh93/ bash/yash语法(也适用zsh):
ssh user@server bash << 'EOF'
command_one
args=(
argument1 argument2 argument3 argument4
argument5 argument6 argument7
)
command_two "${args[@]}"
command_three
EOF
Run Code Online (Sandbox Code Playgroud)