title: incrementing list variable and then loop on it
body: <p>in a sh script, I am trying to make a list of filename in a folder, and then loop on it to check if two consecutive filename respond well to &quot;expression criteria&quot;.</p>
  <p>in a folder I have:
  file1.nii
  file1_mask.nii
  file2.nii
  file2_mask.nii
  etc ...
  undefined number of files. but if filex.nii exists, it must have filex_mask.nii</p>
  <p>in a .txt file that the user modify.
  it contains:</p>
  <blockquote>
  <p>file1.nii tab some parameter \n</p>
  <p>file2.nii tab some parameter \n</p>
  <p>etc ...</p>
  </blockquote>
  <p>the script take long hours after to run, and for example, the mask files are used only after few hours.
  so I want at the beginning of the .sh to check if filenames are well spelled and if any files in the .txt is present in the folder.
  and in case not, stop the .sh and warn the user. not wait hours before noticing the problem.</p>
  <p>For now I tried:</p>
  <pre><code>test=&quot;&quot;
  for entry in &quot;${search_dir}&quot;/*
  do 
    echo  &quot;$entry&quot;
    test=&quot;${test} $entry&quot;
  done
  </code></pre>
  <p>I have then a string variable with space between filenames, but it has the folder name as well.
  ./search_dir/file1.nii ./search_dir/file1_mask.nii
  I wanted file1.nii file1_mask.nii etc ...</p>
  <p>and now I read my .txt file and check if the filename specified in it are in my test variable.</p>
  <pre><code>while read -r line
  do
   set -- $line
   stack=$1
   check=False
   check2=False
   for i in $test; do
     echo &quot;$stack.nii&quot; 
     echo &quot;$i&quot;
     if &quot;${stack}.nii&quot; == &quot;$i&quot;;
      then
        check=True
     fi
     if &quot;${stack}_mask.nii&quot;==&quot;$i&quot;;
      then
        check2=True
     fi
  done 
  </code></pre>
  <p>done &lt; &quot;$txt_file&quot;</p>
  <p>but it is not working.</p>
  <pre><code>&quot;$stack_mask.nii&quot;==&quot;$i&quot;
  </code></pre>
  <p>doesn't seems to be the good way to compare strings
  it generates the error:
  &quot;file1.nii&quot; not found</p>
  <p>Here is my solution for now, based on glenn answer:</p>
  <pre><code>errs=0
  while read -r line; do
    set -- $line
    prefix=&quot;${1}.nii&quot;
    prefix2=&quot;${1}.nii.gz&quot;
    if [ -e ${PATH}/$prefix2 ]; then       
     echo &quot;File found: ${PATH}/$prefix2&quot; &gt;&amp;2
    elif [ -e ${PATH}/$prefix ]; then
     echo &quot;File found: ${PATH}/$prefix&quot; &gt;&amp;2
    else
      echo &quot;File not found: ${PATH}/$prefix&quot; &gt;&amp;2
      errs=$((errs + 1))
    fi
    prefixmask=&quot;${1}_brain_mask.nii&quot;
    prefixmask2=&quot;${1}_brain_maskefsd.nii.gz&quot;
    if [ -e ${PATH}/$prefixmask ]; then
      echo &quot;Mask file found for ${PATH}/$prefixmask&quot; &gt;&amp;2
    elif [ -e ${PATH}/$prefixmask2 ]; then
      echo &quot;Mask file found for ${PATH}/$prefixmask2&quot; &gt;&amp;2    
    else
      echo &quot;Mask file not found: ${PATH}/$prefixmask&quot; &gt;&amp;2
      errs=$((errs + 1))
    fi
  done &lt; &quot;$INPUT&quot;
  echo $errs
  if [ $errs &gt; 0 ]; then
    echo &quot;Errors found&quot;
    exit 3
  fi
  </code></pre>
  <p>then only problem now is that it always exit, even if errs is equal to 0 and I don't know why ...</p>