while循环中碰到ssh的话,如何避免退出

作/译者:叶金荣(Email: email.gif),来源:http://imysql.cn,转载请注明作/译者和出处,并且不能用于商业用途,违者必究。
先看一个例子:
#!/bin/sh
. /root/.bash_profile
cat /home/yejr/alldb|while read LINE
do
   #取得IP和组号
   IP=`echo $LINE | awk '{print $1}'`
   NU=`echo $LINE | awk '{print $2}' | awk -F '-' '{print $1}'`
   cnt=`ssh root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1"`
   echo "$IP $NU $cnt"
done
看起来没有问题吧,实际上,执行的时候只循环了一次,就退出while循环了,为什么呢?
这是因为ssh需要从输入终端来读取数据,在第一次循环时ssh就把 read 读到的数据也给读取了,相当于是被他"吃"了.
解决办法是,指定 ssh 的输入终端,有3种方法:
ssh -f
-f Requests ssh to go to background just before command execution.  This is useful if ssh is going to ask for
   passwords or passphrases, but the user wants it in the background.  This implies -n.  The recommended way to
   start X11 programs at a remote site is with something like ssh -f host xterm.
或者:
ssh -n
-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 automati-
   cally 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.)
或者,将ssh放到后台中执行. 以下是几种写法的综合:
#!/bin/sh
. /root/.bash_profile
cat /home/yejr/alldb|while read LINE
do
   #取得IP和组号
   IP=`echo $LINE | awk '{print $1}'`
   NU=`echo $LINE | awk '{print $2}' | awk -F '-' '{print $1}'`
   #-f
   #cnt=`ssh -f root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1"`
   #-n
   #cnt=`ssh -n root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1"`
   #放后台
   #cnt=`ssh root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1" &`
   #指定输入设备
   cnt=`ssh root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1" 
不知道是否还有其他方法呢?
本文出自 “MySQL中文网”博客 http://www.imysql.cn/
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值