ARJ
ARJ

Reputation: 2080

The `continue` inside `if condition` is not going to next lines of the script

I have a bash script with an if condition and continuous lines of script. However, after if condition nothing in the script seems to run. For example, here are the lines of script (The lines after continue in if condition are not reacting).

 dir=/usr/path/tofiles
    for file in $(find $dir -name \*txt)
    do
        fbase={file%.txt}
        if [ "$step" == "1" -a ! -f "${fbase}.header" ]
            then
                continue
        fi

    ### From here nothing in the script runs #####

        if [ -f "${fbase}merged" ]
        then
                echo "$file already merged"
        fi

       files="$files ${fbase}g.txt.gz"
       cnt=$(($cnt + 1))
       if [ "$cnt" -eq "$chunksize" ]
       then
           outid=$(uuidgen)
           logfile=${outdir}/${outid}.cluster.log
           echo "sh $(pwd)/mysecript.sh $outid $files"  
       fi
    done

After the first if condition nothing in the script is running, I tried printing using echo nothing is showing up. Precisely, the issue is after the continue statement within the if condition. Any help/suggestions are much appreciated.

Thanking you

Upvotes: 0

Views: 13850

Answers (2)

avermaet
avermaet

Reputation: 1593

The problem with your script is, that in case your first if statement evaluates always to true you'll always skip the rest of the loop, due to the continue, so nothing else will be executed. This behavior is the same as in all other programming languages.

  • continue, like break, is a keyword to control the loop behavior. This means that using continueit is possible to skip the rest of the current loop iteration. And using break it is possible to exit the loop.

In case you need to go further to the next if statement, you need to nest your ifs, so that both are checked/evaluated.

More background information regarding this issue can be found here.

According to your comments, your code should include nested ifs, like:

dir=/usr/path/tofiles
  for file in $(find $dir -name \*txt)
  do
    fbase={file%.txt}

    if [ "$step" == "1" -a ! -f "${fbase}.header" ]
    then
      if [ -f "${fbase}merged" ]
      then
        echo "$file already merged"
      fi

      files="$files ${fbase}g.txt.gz"
      cnt=$(($cnt + 1))
      if [ "$cnt" -eq "$chunksize" ]
      then
        outid=$(uuidgen)
        logfile=${outdir}/${outid}.cluster.log
        echo "sh $(pwd)/mysecript.sh $outid $files"  
      fi
    fi
done

Upvotes: 1

iamsaksham
iamsaksham

Reputation: 2949

You seem to have a wrong interpretation of the continue statements.

The continue statement skips the lines below it and starts with the next iteration.

while (true) {
  print(1);
  if (true) {
    continue;
  }
  print(2);
}

In the above print(2) will never get executed as it skips it everytime and starts with the next iteration.

For deeper insight please read Nested-If statements in Shell-scripting

For your scenario please try this

dir=/usr/path/tofiles
    for file in $(find $dir -name \*txt)
    do
        fbase={file%.txt}
        if ! [ "$step" == "1" -a ! -f "${fbase}.header" ]
        then
            if [ -f "${fbase}merged" ]
            then
                echo "$file already merged"
            fi

            files="$files ${fbase}g.txt.gz"
            cnt=$(($cnt + 1))
            if [ "$cnt" -eq "$chunksize" ]
            then
                outid=$(uuidgen)
                logfile=${outdir}/${outid}.cluster.log
                echo "sh $(pwd)/mysecript.sh $outid $files"  
            fi
        fi
    done

Upvotes: 1

Related Questions