关于ssh -n,man 提示为:redirects stdin from /dev/null. This must be used when ssh is run in the background.
有人知道大概是什么意思吗?
因为我有这样一个问题:
复制代码
复制代码
有人知道大概是什么意思吗?
因为我有这样一个问题:
- while read input
- do
- file=$(echo $input |awk '{print $1}')
- md5=$(echo $input | awk '{print $2}')
- scp /data/$file root@10.1.1.2:/data
- md5_curr=`ssh root@10.1.1.2 "md5sum /data/$file" | awk '{print $1}'`
- if [ $md5 != $md5_curr ];then
- echo "MD5 Error"
- fi
- done < /data/a.txt
- # cat /data/a.txt
- 1.txt asdfasdfasdfadsf
- 2.txt dafsadsfasfdagff
这个脚本只能够scp 1.txt,但read 第二行时显示为空。
必须在ssh,上加上-n才可以read第二行。是不是stdin变了,不再是/data/a.txt??
------------------
因为连接没有断开
而-n会运行完毕后,自动断开连接
---------------------
ssh时,ssh从while里继承stdin为/data/a.txt,然后把值读取了,但这些值对它来说没意义,所以被忽略了,然后继续执行md5sum的命令。这时while再从/data/a.txt读时,已经为空。
-n 后设定ssh的stdin为/dev/null,这样就不会继承while的stdin???
-------------------
这就是原因,这个while重定向会先exec n<&0把标准输入copy一份存起来,然后exec 1<$servers_filename,然后开始循环,但你循环内部没有指定-n,ssh命令默认读标准输入就是把文件读了。
但我完全没发现ssh会读标准输入啊,不知道了。