[英]Shell script with ssh command with an if statement
I want to write a shell script to do the following: 我想编写一个shell脚本来执行以下操作:
I tried something like this, but it doesn't work.. any help would be much appreciated. 我尝试过类似的方法,但是它不起作用..任何帮助将不胜感激。 Thanks!
谢谢!
#! /bin/bash
dir1=dirname
runNo=0
for i in 1 2
do
runNo=$(($runNo+1))
echo "host$i runNo$runNo"
ssh host$i "cd /tmp;
if [ -d $dir1 ]; then
cd $dir1;
else
mkdir $dir1;
cd $dir1;
fi;
cp ~/local/file$runNo file"
echo "done"
done
How about 怎么样
ssh host$i "mkdir -p /tmp/$dir1"
scp ~/local/file$runNo host$i:/tmp/$dir1/file
mkdir -p
suppresses errors if the directory already exists. 如果该目录已存在,则
mkdir -p
抑制错误。
Like this perhaps? 也许这样?
runNo=1
for h in host1 host2; do
cat ~/local/file$runNo |
ssh "$h" 'mkdir -p /tmp/dirname; cat >/tmp/dirname/file'
runNo=$(($runNo+1))
done
If I interpret your description correctly, the cd
is superfluous. 如果我正确解释了您的描述,则该
cd
是多余的。
If Bourne shell compatibility is necessary, you can replace the increment with 如果需要Bourne shell兼容性,则可以将增量替换为
runNo=$(expr $runNo + 1)
Try this: 尝试这个:
ssh -t -t host$i <<eod1
echo "your command goes here"
add eod1 to terminate your ssh
eod1
Note two -t -t is required to overwrite the sudo terminal error 注意两个-t -t是覆盖sudo终端错误的必要条件
this editing rules is driving me crazy 这个编辑规则让我发疯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.