title: Option from function called unintentionally, ignoring argument value, invalid argument and how to find largest file in directory and get its' size
body: <p>An option from my script is being called into an echo where I don't want it to be called.
  When I run the script with the option -r, it's called in the echo of &quot;directory that doesn't exist.&quot;
  So for example I run bash sdel.sh -r [dir] and even if that directory exists and is moved to trash directory, I still get the error message saying -r doesn't exist. If it doesn't exist I get 2 messages, 1 that says that directory doesn't exist and 1 that says -r doesn't exist.</p>
  <p>Parameter processing:
  ( -r fixed)</p>
  <pre><code>while getopts &quot;hr:t:s:u:&quot; options; do
  case $options in
  ...
  # shift $((OPTIND-1))
  
  r) tgtdir=&quot;${OPTARG}&quot;
  
    if [ ! -d &quot;${tgtdir}&quot; ]; then
      echo &quot;$0: error: tried to delete directory that does not exist: ${tgtdir}&quot;
  #   shift
      continue
    fi
    tarname=&quot;${tgtdir}.tar&quot;
    tar -cf &quot;$tarname&quot; &quot;${tgtdir}&quot;
    mv &quot;$tarname&quot; $trash
    rm -r &quot;${tgtdir}&quot;
  # shift
  
  exit;; 
  
  esac
  done
  </code></pre>
  <p>In another option, t, I'm trying to delete files over x hours from user input but it always deletes whatever the value I put in. (FIXED)</p>
  <pre><code>t)
  tgtdir=&quot;${OPTARG}&quot;
  now = `date +%s`
  cd $trash
  for f in `ls`; do
  fileage=$(stat -c '%Y' &quot;$f&quot;)
  
  if [ $((now-fileage)) -gt $((60*60*${tgtdir})) ]; then
  rm -rf &quot;$f&quot;
  echo &quot;Cleaning directory $trash&quot;
  
  else
  echo &quot;Nothing to clean in directory $trash&quot;
  fi
  done
  exit;;
  </code></pre>
  <p>Another one of them, s, is to find and delete files over x size in KB in the $trash directory but I'm getting an invalid argument when I try to run it.</p>
  <p>(FIXED)</p>
  <pre><code>s)
  tgtdir=&quot;${OPTARG}&quot;
  cd $trash
  find . -type f -size +${tgtdir}k -delete
  exit;;
  </code></pre>
  <p>In u, trying to get largest file in directory and its' size but currently, it still also gives me the directory back and I wanted it to be just the file. (FIXED)</p>
  <pre><code>u) find $trash -type f -printf '%s %p\n' | sort -nr | head-1
  exit;;
  </code></pre>
  <p>Help's appreciated in solving each of these issues!</p>