title: Refactoring my shell scipts : calling a script and redirect output to a new file inside a loop
body: <p>I have a shell script that uses the sed command to transform an input file to a new output file, this script works when I call :</p>
  <pre><code>./sed_command.sh input_file &gt; output_file
  </code></pre>
  <p>here is my script :</p>
  <pre><code>#!/usr/bin/env sh
  
  while read line
    do
      # Split each line based on ; separator
      transposed=$(echo &quot;$line&quot; | sed -e &quot;s/;/\\n/g&quot;)
      res=$(echo &quot;$transposed&quot; | sed '/${GDATEF(\([^,]*\),ddMMyyyy)}/{
              # Split the string in two part
              s//\1/g
              # # parse the input for GNU date input format
              s/D/ day/g
              # Handle shell quoting
              '&quot;s/'/\\''\\'/&quot;'
               # pass and execute date
              s/.*/date -d &quot;now &quot;'\''&amp;'\'' +%d%m%Y/e
          }')
      oneline=$(echo &quot;$res&quot; | sed -z &quot;s/\n/;/g&quot; | sed 's/.$//')
      echo $oneline
    done &lt; $1
  
  </code></pre>
  <p>Now I need to call that script inside a double loop that browse all directories in a folder :</p>
  <pre><code>cd /etc/newman/Newman
  
  for team in *;do
        if  [ -d &quot;$team&quot; ]; then
            echo &quot;team=$team&quot;
            cd $team
            for scope in *;do
                if [ -d &quot;$scope&quot; ]; then
                    echo &quot;scope=$scope&quot;
                    CSV_FILE=$(ls /etc/newman/Newman/$team/$scope/*.csv -1  || true)
                    echo &quot;Transforming : ${CSV_FILE}&quot;
                    if [ ! -z &quot;${CSV_FILE}&quot; ] ; then sh sed_command.sh  ${CSV_FILE} &gt; ${CSV_FILE}_Newman; else echo &quot;No CSV files to transform&quot;; fi
  
  
                fi
            done
            cd ..
        fi
      done
  
  </code></pre>
  <p>I have tested the second script looping works fine the only issue is when I call sed_command.sh inside the second script I get theses two errors :</p>
  <pre><code>: not found.sh: line 2:
  sed_command.sh: line 19: syntax error: unexpected &quot;done&quot; (expecting &quot;do&quot;)
  </code></pre>
  <p>I guess redirection does not work the same way inside a loop? thanks for help</p>