The pitfalls in shell while loop referencing SSH commands

Source: Internet
Author: User

The principle shell code is as follows:

#!/bin/shcat ../androidsrc | while read linedo        ip=$(echo $line | awk ‘{print $1}‘)        srcdir=$(echo $line | awk ‘{print $2}‘)        destdir=$(echo $line | awk ‘{print $3}‘)        user=$(echo $line | awk ‘{print $4}‘)        port=$(echo $line | awk ‘{print $5}‘)        build=$(echo $line | awk ‘{print $6}‘)        echo $ip"   "$srcdir"   "$destdir"    "$user"    "$port"    "$build        #rsync -av $srcdir "-e ssh -p $port" [email protected]$ip:$destdir        ssh -p $port [email protected]$ip sh $builddone

The androidsrc file is as follows:

xxx.yyy.zzz.zzz /data/build/xgame2/src/ /data/build/xgame/src/ sggame 61693 /data/build/buildgame.shxxx.yyy.zzz.zzz /data/build/xgame2/src/ /data/build/xgame-ios/src/ sggame 61693 /data/build/buildgame2.shxxx.yyy.zzz.zzz /data/build/xgame2/src/ /data/build/xgame/src/ sggame 61693 /data/build/buildgame.shxxx.yyy.zzz.zzz /data/build/xgame2/src/ /data/build/xgame2/src/ sggame 61693 /data/build/buildgame2.shxxx.yyy.zzz.zzz  /data/build/xgame2/src/ /data/build/xgame/src/ sggame 61693 /data/build/buildgame.shxxx.yyy.zzz.zzz  /data/build/xgame2/src/ /data/build/xgame/src/ sggame 61693 /data/build/buildgame.shxxx.yyy.zzz.zzz /data/build/xgame2/src/ /data/build/xgame/src/ sggame 61693 /data/build/buildgame.shxxx.yyy.zzz.zzz  /data/build/xgame2/src/ /data/build/xgame/src/ sggame 61693 /data/build/buildgame.shxxx.yyy.zzz.zzz /data/build/xgame2/src/ /data/build/xgame/src/ sggame 61693 /data/build/buildgame.shxxx.yyy.zzz.zzz  /data/build/xgame2/src/ /data/build/xgame/src/ root 22 /data/build/buildgame.sh

Xxx. yyy. Zzz. Zzz indicates the IP address.

Problem Source: After executing this script, it is found that the loop is interrupted after only the first execution.

Problem resolution: After query, we found that the contents of the androidsrc file that has been in the pipeline or redirected were eaten in advance by SSH-p $ port [email protected] $ IP sh $ build.


The final modification is as follows:

#!/bin/shcat ../androidsrc | while read linedo        ip=$(echo $line | awk ‘{print $1}‘)        srcdir=$(echo $line | awk ‘{print $2}‘)        destdir=$(echo $line | awk ‘{print $3}‘)        user=$(echo $line | awk ‘{print $4}‘)        port=$(echo $line | awk ‘{print $5}‘)        build=$(echo $line | awk ‘{print $6}‘)        echo $ip"   "$srcdir"   "$destdir"    "$user"    "$port"    "$build        #rsync -av $srcdir "-e ssh -p $port" [email protected]$ip:$destdir        ssh -p $port [email protected]$ip sh $build < /dev/nulldone


Conclusion: During the while read operation, this issue should be taken into account if the standard input commands such as CAT, mail, ssh, grep, sed, and awk are processed. Otherwise, shell writing is a pitfall.




This article is from the "Technology for the Future" blog and is not reposted!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.