info ssh
-n Redirects stdin from /dev/null (actually, prevents reading from
stdin). This must be used when ssh is run in the background. A
common trick is to use this to run X11 programs on a remote
machine. For example, ssh -n shadows.cs.hut.fi emacs & will
start an emacs on shadows.cs.hut.fi, and the X11 connection will
be automatically forwarded over an encrypted channel. The ssh
program will be put in the background. (This does not work if
ssh needs to ask for a password or passphrase; see also the -f
option.)
[root@k8s-master ansible]# cat csren.sh
#! /bin/bash
while read line
do
echo $line
ssh -q compute-1 'date'
done << EOF
11
22
32
42
52
EOF
[root@k8s-master ansible]# cat csren.sh
#! /bin/bash
while read line
do
echo $line
ssh -q compute-1 'date'
done < 1.txt
[root@k8s-master ansible]# cat csren.sh
#! /bin/bash
while read line
do
echo $line
ssh -q compute-1 'date'
done < $1或者$i
[root@k8s-master ansible]# cat csren11.sh
#! /bin/bash
cat $hostfile | while read line
do
echo $line ":"
done
##执行脚本,输出结果只是打印了第一行11,没有打印所有的循环,也就是只跑了一遍
[root@k8s-master ansible]# bash csren.sh
11
Mon May 9 01:33:46 JST 2022
分析原因:
当ssh和while一起使用时,ssh在执行时会进行读取所有的输入(也就是第一次就把11-52全部读取),导致在第二次执行时没有内容可读,因此会退出while循环。while中使用了重定向机制,input中的内容都已经读入并重定向给了整个while语句。
重定向的while里要避免使用接收标准输入的命令。
解决办法:
改为for循环或者使用下面的方法:
ssh -n command等同于 < /dev/null
1. SSH 加-n 选项:
[root@k8s-master ansible]# cat csren.sh
#! /bin/bash
while read line
do
echo $line
ssh -q -n compute-1 'date'
done << EOF
11
22
32
42
52
EOF
[root@k8s-master ansible]# bash csren.sh
11
Mon May 9 01:39:27 JST 2022
22
Mon May 9 01:39:27 JST 2022
32
Mon May 9 01:39:27 JST 2022
42
Mon May 9 01:39:27 JST 2022
52
Mon May 9 01:39:28 JST 2022
2. 使用
要想分析原因可以通过:
truss, strace, ltrace工具
truss -o fenxi.log bash csren.sh
strace -f -o fenxi.log bash csren.sh
-f: 除了跟踪当前进程外,还跟踪子进程
-o: 将输出信息写到文件file中
ltrace -p 234: 跟踪一个pid为234的已经在运行的进程
while 读取文件以及变量
- 当只有一个变量的时候,传入
[root@k8s-master bash]# cat test.txt
12 34 qazwsx
34 56 esdacrc
wesd ds wsaa
[root@k8s-master bash]# cat while.sh
#! /bin/bash
cat test.txt | while read line1; #line1 依次取的是test.txt 文件中的每行内容,,当只有一个变量的时候
do
echo $line1
done
##文件有三行,循环三次,所以打印了三遍
[root@k8s-master bash]# bash while.sh
12 34 qazwsx
34 56 esdacrc
wesd ds wsaa
- 当传入两个变量的时候,读取
#! /bin/bash
cat test.txt | while read line1 line2; #
do
echo "the first parameter: $line1"
echo "the second parameter: $line2"
done
[root@k8s-master bash]# bash while.sh
##第一次循环的时候,因为只传两个参数,默认以空格分割,
##12 34 qazwsx要分成两部分,第一部分是“12”;第二部分是“34 qazwsx”
the first parameter: 12
the second parameter: 34 qazwsx
##第二次循环
the first parameter: 34
the second parameter: 56 esdacrc
##第三次循环
the first parameter: wesd
the second parameter: ds wsaa
###while read line;do echo $line;done < file这种效率最高,如果涉及到ssh连接server的时候,记得加-n或者在ssh那行最后加< /dev/null
for 循环
在读取文件时,for i in cat xx.txt 或者 for i in $(
[root@k8s-master bash]# cat test.txt
12 34 qazwsx
34 56 esdacrc
wesd ds wsaa
##for 循环,默认以空格读取文件中所有内容,挨个打印出来
[root@k8s-master bash]# cat for.sh
#! /bin/bash
for i in `cat test.txt`
do
echo "the first parameter: $i"
done
[root@k8s-master bash]#
[root@k8s-master bash]# bash for.sh
the first parameter: 12
the first parameter: 34
the first parameter: qazwsx
the first parameter: 34
the first parameter: 56
the first parameter: esdacrc
the first parameter: wesd
the first parameter: ds
the first parameter: wsaa
[root@k8s-master bash]# cat for.sh
#! /bin/bash
for i in $(



