title: Error in sh eval code, "$: not found"?
body: <p>I have a bash script written for our DD-WRT routers, it restarts the NAS daemons periodically to fix random problems with iPhone's connecting to wireless. </p>
  
  <p>This is one of my first bash scripts, and I'm lost trying to figure out how to identify the root of this problem. </p>
  
  <p>The script does run, and it does restart the services as it's supposed to. However, I am getting the error: <code>eval: line 1: $: not found</code> on the terminal when the script runs.</p>
  
  <pre><code>
  #!/bin/sh
  
  ##
  # Restarts the nas daemon on the specified interval
  ##
  
  # Restart interval, in seconds
  #T=3600 # hourly
  T=60 # for testing
  
  # Log file
  if [ $# -ne 0 ]; then
          log=$1
  else
          log=/tmp/restart.log
  fi
  
  while [ true ]; do
          # Wait
          sleep $T
  
          # Find commands to relaunch all nas daemons
          nascmd=`ps ww | grep nas | awk '{if($5!="grep"){$1=$2=$3=$4=""; print $0";"}}'`
          echo [`date`] Existing pid: `ps | grep nas | awk '{ORS=",";if($5!="grep"){print $1}}'` >> $log
  
          # Restart nas with original arguments
          killall -HUP nas
          echo [`date`] Running command: $nascmd >> $log
  
          # Strip special characters prior to eval
          safecmd=`echo $nascmd | sed 's/\$/\\$/g'`
          eval $safecmd
  
          echo [`date`] Finished, new pid: `ps | grep nas | awk '{ORS=",";if($5!="grep"){print $1}}'` >> $log
  done
  </code></pre>
  
  <p>I presume the error originates from <code>eval</code> since it's exactly 1 line long. I'm trying to figure out what <code>$</code> is and why it's not found. I escape all the dollar signs, if they exist. There shouldn't be any, however, I was just being safe in case someone changes the encryption pass phrase to something with a dollar sign.</p>