title: Shell function to execute command and redirect output
body: <p>I am not so familiar with shell script syntax and protocols.</p>
  
  <p>I have written following function which accepts </p>
  
  <ul>
  <li>a command string as a mandetory parameter</li>
  <li>ignore error as an optional parameter</li>
  </ul>
  
  <p>--</p>
  
  <pre><code>function quit {
      \rm -f "~/script.lock"
      exit
  }
  
  function abnormal_quit {
      echo $'\n'
      echo "Script Execution Terminated Abnormally.."
      echo "STATUS :: FAIL"
      quit
  }
  
  function exec_cmd {
      command="$1"
      continue_on_error="true"
      if [ -z "$2" ]; then
          continue_on_error="false"
      fi
  
      echo "========================================================"
      echo "Executing command :-"
      echo "$command ....."
  
      if ${command[@]}
      then
          echo "Command Executed successfully with return code : $?"
          echo "COMMAND - $command"
          echo "==============================================================="
          echo $'\n'
      else
          echo "Failed to execute command with return code :- $?"
          echo "COMMAND - $command"
          echo "==============================================================="
          echo $'\n'
          if [ $continue_on_error == "false" ]; then
              abnormal_quit
          fi
      fi
  }
  
  log_file="output.log"
  exec_cmd "ls -lrt &gt;&gt; $log_file"
  </code></pre>
  
  <p>If I execute above shell script it gives me following error</p>
  
  <pre><code>[root@localhost data]# sh test.sh
  ========================================================
  Executing command :-
  ls -lrt &gt;&gt; log.out .....
  ls: &gt;&gt;: No such file or directory
  ls: log.out: No such file or directory
  Failed to execute command with return code :- 2
  COMMAND - ls -lrt &gt;&gt; log.out
  ===============================================================
  
  Script Execution Terminated Abnormally..
  STATUS :: FAIL
  </code></pre>
  
  <p>The issue here is - The shell script assume "ls -lrt >> log.out" as a single command and the redirection arrows are considered as an filename argument to the "ls" command. Hence throws an error ">>: No such file or directory"</p>