ssh导致while read line出错

2018-03-20  本文已影响19人  shuff1e
     -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.)
#/bin/bash
IPS="10.1.1.10 3001
10.1.1.10 3003
10.1.1.11 3001
10.1.1.11 3002
10.1.1.11 3004
10.1.1.11 3005
10.1.1.13 3002
10.1.1.13 3003
10.1.1.13 3004
10.1.1.14 3002"
echo "====while test ===="
i=0

echo $IPS | while read line
do
    echo $(($i+1))
    echo $line
done


echo "====for test ===="
n=0
for ip in $IPS ;
do
   n=$(($n+1))
   echo $ip
   echo $n
done

输出结果如下:

====while test ====
1
10.1.1.10 3001 10.1.1.10 3003 10.1.1.11 3001 10.1.1.11 3002 10.1.1.11 3004 10.1.1.11 3005 10.1.1.13 3002 10.1.1.13 3003 10.1.1.13 3004 10.1.1.14 3002
====for test ====
10.1.1.10
1
3001
2
10.1.1.10
3
3003
4
10.1.1.11
5
3001
6
10.1.1.11
.... 
上一篇 下一篇

猜你喜欢

热点阅读