Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

if statement - Execution sequence in loop with if condition

In my loop below the instructions are not executed one after the other, but out of order. Some do not wait until the previous ones are finished to execute. For example all displays with echo are done in advance when it loops. I tried to put & at the end of each instruction it didn't work

My loop contains if condition

Thanks for your help.

#!/bin/bash
INPUT=Mnt.csv
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read dom alias port ver
do
ssh -n $alias "
 echo =======================================================
               echo $alias
 echo =======================================================

                        if [ ${ver} = "v3" ]
                        then
               

                       instruction 1
                        ........
                        ........
                        ........
                       instruction n

                       else
instruction A
........
........
........
instruction X

                        
                        fi
               exit" & >> $(date +%Y%m%d).log &
question from:https://stackoverflow.com/questions/65889177/execution-sequence-in-loop-with-if-condition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It workes by adding wait like this

#!/bin/bash
INPUT=Mnt.csv
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read dom alias port ver
do
ssh -n $alias "
 echo =======================================================
               echo $alias
 echo =======================================================

                        if [ ${ver} = "v3" ]
                        then
               

                       instruction 1
                        ........
                        ........
                        ........
                       instruction n

                       else
instruction A
........
........
........
instruction X

                        
                        fi
                        wait
               exit"  >> $(date +%Y%m%d).log 

wait


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...