[英]Error when stacking SSH command arguments within a bash script using other scripts as variables
I have a csv file called addresses.csv which looks like this,我有一个名为addresses.csv的csv文件,看起来像这样,
node-1,xx.xxx.xx.xx,us-central-a
....
node-9,xxx.xx.xxx.xx,us-east1-a
I have a script below called 0run.sh,我在下面有一个名为 0run.sh 的脚本,
#!/bin/bash
username='user'
persist="bash /home/${username}/Documents/scripts/disk/persistentDisk.sh"
first="bash /home/${username}/Documents/scripts/disk/firstAttach.sh"
while IFS=, read -r int ip <&3; do
if [ "$int" == "node-1" ]; then
--->ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l ${username} ${ip} "${persist}; ${first}"<---
else
ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l ${username} ${ip} "${first}"
fi
done 3<addresses.csv
The error occurs in the part of the code where I drew the arrows.错误发生在我绘制箭头的代码部分。
When it runs on node-1
, instead of running ..persistentDisk.sh
followed by ..firstAttach.sh
, it only runs ..persistentDisk.sh
and gives me the following error before it runs ..persistentDisk
.当它在node-1
上运行时,而不是运行..persistentDisk.sh
后跟..firstAttach.sh
,它只运行..persistentDisk.sh
并在运行..persistentDisk
之前给我以下错误。
bash: /home/user/Documents/scripts/disk/firstAttach.sh: No such file or directory
The rest of the script runs completely fine.脚本的 rest 运行完全正常。 The only error occurs at this one part where it misses the 2nd script.唯一的错误发生在这一部分,它错过了第二个脚本。
When I run the command like this it runs fine.当我像这样运行命令时,它运行良好。
ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l ${username} ${ext} "${first}"
When I run it like this, it runs fine as well.当我这样运行它时,它也运行良好。
ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l user xxx.xx.xxx.xx "bash /home/${username}/Documents/scripts/disk/persistentDisk.sh; bash /home/${username}/Documents/scripts/disk/firstAttach.sh"
When I run the command like with a \
before the ;
当我像在;
之前使用\
一样运行命令时to escape it like this,像这样逃避它,
ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l ${username} ${ext} "${persist}\; ${first}"
I get the following error, and neither scripts run within the node-1
part of the code, but the rest of the code's else loops run fine.我收到以下错误,并且两个脚本都没有在代码的node-1
部分中运行,但是代码的 else 循环的 rest 运行良好。
bash: /home/user/Documents/scripts/disk/persistentDisk.sh;: No such file or directory
Why can't I stack the 2 commands within the if statement in the ssh using variables?为什么我不能使用变量在 ssh 的 if 语句中堆叠 2 个命令?
If I clearly understand: your real problem consist to leave STDIN
free for interaction in target host!如果我清楚地理解:您真正的问题在于让STDIN
自由以在目标主机中进行交互!
read
and redirection关于read
和重定向Try using:尝试使用:
#!/bin/bash
username='user'
persist="bash /home/${username}/Documents/scripts/disk/persistentDisk.sh"
first="bash /home/${username}/Documents/scripts/disk/firstAttach.sh"
while IFS=, read -r -u $list int ip foo; do
if [ "$int" == "node-1" ]; then
echo CMD... $ip, $persist
else
[ "$ip" ] && echo CMD... $ip, $first
fi
done {list}<addresses.csv
Tested, this èroduce:经过测试,这 èroduce:
CMD... xx.xxx.xx.xx, bash /home/user/Documents/scripts/disk/persistentDisk.sh
CMD... xxx.xx.xxx.xx, bash /home/user/Documents/scripts/disk/firstAttach.sh
-u
flag to read, tell to use file descriptor ${list}
instead of STDIN
-u
读取标志,告诉使用文件描述符${list}
而不是STDIN
foo
is some useless variable used to prevent rest of line to be stored in $ip
( xx.xxx.xx.xx,us-central-a
in this case) foo
是一些无用的变量,用于防止将行的 rest 存储在$ip
( xx.xxx.xx.xx,us-central-a
在这种情况下){list}</path/to/filename
create a new variable by finding any free file descriptor . {list}</path/to/filename
通过查找任何空闲文件描述符来创建一个新变量。ssh
(and redirection)关于ssh
(和重定向)You could use:你可以使用:
#!/bin/bash
username='user'
persist="/home/${username}/Documents/scripts/disk/persistentDisk.sh"
first="/home/${username}/Documents/scripts/disk/firstAttach.sh"
while IFS=, read -r -u $list int ip foo; do
[ "$int" = "node-1" ] && cmd=persist || cmd=first
[ "$ip" ] && ssh -i ~/.ssh/key -t -o StrictHostKeyChecking=no \
-l ${username} ${ext} /bin/bash "${!cmd}"
done {list}<addresses.csv
By using this syntax, you will keep STDIN
free for script running on target host.通过使用此语法,您将保持STDIN
自由,以便在目标主机上运行脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.