title: Bash script function call error
body: <p>I am writing my first Bash script and am running into a syntax issue with a function call.</p>
  
  <p>Specifically, I want to invoke my script like so:</p>
  
  <pre><code>sh myscript.sh -d=&lt;abc&gt;
  </code></pre>
  
  <p>Where <code>&lt;abc&gt;</code> is the name of a specific directory inside of a fixed parent directory (<code>~/app/dropzone</code>). If the child <code>&lt;abc&gt;</code> directory doesn't exist, I want the script to create it before going to that directory. If the user doesn't invoke the script with a <code>-d</code> argument at all, I want the script to exist with a simple usage message. Here's my best attempt at the script so far:</p>
  
  <pre><code>#!/bin/bash
  dropzone="~/app/dropzone"
  
  # If the directory the script user specified exists, overwrite dropzone value with full path
  # to directory. If the directory doesn't exist, first create it. If user failed to specify
  # -d=&lt;someDirName&gt;, exit the script with a usage statement.
  validate_args() {
      args=$(getopt d: "$*")
      set -- $args
      dir=$2
      if [ "$dir" ]
      then
          if [ ! -d "${dropzone}/targets/$dir" ]
          then
              mkdir ${dropzone}/targets/$dir
          fi
          dropzone=${dropzone}/targets/$dir
      else
          usage
      fi
  }
  
  usage() {
      echo "Usage: $0" &gt;&amp;2
      exit 1
  }
  
  # Validate script arguments.
  validate_args $1
  
  # Go to the dropzone directory.
  cd dropzone
  echo "Arrived at dropzone $dropzone."
  
  # The script will now do other stuff, now that we're in the "dropzone".
  # ...etc.
  </code></pre>
  
  <p>When I try running this I get the following error:</p>
  
  <pre><code>myUser@myMachine:~/app/scripts$ sh myscript.sh -dyoyo
  mkdir: cannot create directory `/home/myUser/app/dropzone/targets/yoyo': No such file or directory
  myscript.sh: 33: cd: can't cd to dropzone
  Arrived at dropzone /home/myUser/app/dropzone/targets/yoyo.
  </code></pre>
  
  <p>Where am I going wrong, and is my general approach even correct? Thanks in advance!</p>