title: Bash - sometimes creates only empty output
body: <p>I am trying to create a bash dictionary script that accepts first argument and creates file named after that, then script accepts next arguments (which are files inside same folder) and outputs their content into file (first argument). It also sorts, deletes symbols etc., but main problem is, that sometimes ouptut file is empty (I am passing one non empty file and one non existing file), after deleting and running script few more times it is sometimes empty sometimes not.</p>
  
  <pre><code>#!/bin/bash
  
  numberoffileargs=$(( $# - 1 ))
  exitstat=0
  counterexit=0
  acceptingstdin=0;
  &gt; "$1";
  #check if we have given input files given
  if [ "$#" -gt 1 ]; then
      #for cycle going through input files
      for i in "${@:2}"
          do
      #check whether input file is readable
              if [ -r "${i}" ]; then
                  cat "${i}" &gt;&gt; "$1"      
      #else redirect to standard output
              else
              exitstat=2
              counterexit=$((counterexit + 1))
              echo "file does not exist" 1&gt;&amp;2
              fi
         done 
  else
      echo "stdin code to be done"
      acceptingstdin=1
          #stdin input to output file
  #stdin=$(cat)
  fi
      #one word for each line, alphabetical sort, alphabet only, remove duplicates
  #all lowercase
  #sort -u &gt;&gt; "$1"
  
  
  
  if [ "$counterexit" -eq "$numberoffileargs" ] &amp;&amp; [ "$acceptingstdin" -eq 0 ]; then 
      exitstat=3
  fi
  cat "$1" | sed -r 's/[^a-zA-Z\-]+/ /g' | tr A-Z a-z | tr ' ' '\n' | sort -u | sed '/^$/d'     &gt; "$1"
  echo "$numberoffileargs"
  echo "$counterexit"
  echo "$exitstat"
  exit  $exitstat
  </code></pre>