Today we find a problem: Read the following script: [email protected]/0 # cat ! $cat /tmp/test_nginxls *.txt | tr ' ' ' \ n ' | while read linedo echo $lineif [ ' Yes ' == ' yes ' ]thenssh 192.168.109.10 ' echo ' yes ' "elsessh 192.168.109.10 "echo ' no '" fidone the script above is simple, read all the TXT-terminated files in the current directory (actually I just get the loop number), but we run the script to see that the script exits only once. The effect is as follows:[email protected]/0 # bash -x /tmp/test_nginx + tr ' ' ' \ n ' + ls 1.txt 2.txt ip.txt png.txt + read line+ echo 1.txt1.txt+ ' [' yes = = yes ' + ssh 192.168.9.10 ' echo ' ' yes ' ' yes+ read line now congratulations, You have come into contact with the ssh and while conflict! The approach is always more problematic. The solution is simple: Method one: ssh 192.168.109.10 "echo ' yes '" changed to &NBSP; ssh 192.168.109.10 "echo ' Yes '" < /dev/null method two: ssh 192.168.109.10 "echo ' yes '" change to ssh -n 192.168.109.10 "echo ' yes '" causes:ssh The command reads a loop that is not executed while the following content can help you understand it. [Email protected]/0 # bash -x /tmp/test_nginx + ls 1.txt 2.txt ip.txt png.txt+ tr ' ' ' \ n ' + read line+ echo 1.txt1.txt+ ' [' yes == yes '] ' + ssh 192.168.9.10 cat //we change the order to cat, we'll find out. 2.txtip.txtpng.txt+ read line
This article is from the Linux system maintenance blog, so be sure to keep this source http://linuxadmin.blog.51cto.com/2683824/1885184
Shell script Execution Conflict event-ssh&while