title: Improve my password generation script
body: <p>I have created a little password generation script. I'm curious to what improvements can be made for it <strong>except</strong> input error handling, usage information etc. It's the core functionality I'm interested in seeing improvements upon.</p>
  
  <p>This is what it does (and what I like it to do):</p>
  
  <ol>
  <li>Keep it easy to change which Lowercase characters (L), Uppercase characters (U), Numbers (N) and Symbols (S) that are used in passwords.</li>
  <li>I'd like it to find a new password of legnth 10 for me in max two seconds.</li>
  <li>It should take a variable length of the password string as an argument.</li>
  <li>Only a password containing at least one L, U, N and S should be accepted.</li>
  </ol>
  
  <p>Here is the code:</p>
  
  <pre><code>#!/bin/bash
  
  PASSWORDLENGTH=$1
  RNDSOURCE=/dev/urandom
  L="acdefghjkmnpqrtuvwxy"
  U="ABDEFGHJLQRTY"
  N="012345679"
  S="\-/\\)?=+.%#"
  
  until [ $(echo $password | grep [$L] | grep [$U] | grep [$N] | grep -c [$S] ) == 1 ]; do
      password=$(cat $RNDSOURCE | tr -cd "$L$U$N$S" | head -c $PASSWORDLENGTH)
      echo In progress: $password # It's simply for debug purposes, ignore it
  done
  echo Final password: $password
  </code></pre>
  
  <p><strong>My questions are:</strong></p>
  
  <ul>
  <li>Is there a nicer way of checking if the password is acceptable than the way I'm doing it?</li>
  <li>What about the actual password generation?</li>
  <li>Any coding style improvements? (The short variable names are temporary. Though I'm using uppercase names for "constants" [I know there formally are none] and lowercase for variables. Do you like it?)</li>
  </ul>
  
  <p>Let's vote on the most improved version. :-)</p>
  
  <p>For me it was just an exercise mostly for fun and as a learning experience, albeit I will start using it instead of the generation from KeepassX which I'm using now. It will be interesting to see which improvements and suggestions will come from more experienced Bashistas (I made that word up).</p>
  
  <hr>
  
  <p>I created a little basic script to measure performance: (In case someone thinks it's fun)</p>
  
  <pre><code>#!/bin/bash
  
  SAMPLES=100
  SCALE=3
  
  echo -e "PL\tMax\tMin\tAvg"
  for p in $(seq 4 50); do
      bcstr=""; max=-98765; min=98765
      for s in $(seq 1 $SAMPLES); do
          gt=$(\time -f %e ./genpassw.sh $p 2&gt;&amp;1 1&gt;/dev/null)
          bcstr="$gt + $bcstr"
          max=$(echo "if($max &lt; $gt ) $gt else $max" | bc)
          min=$(echo "if($min &gt; $gt ) $gt else $min" | bc)
      done
      bcstr="scale=$SCALE;($bcstr 0)/$SAMPLES"
      avg=$(echo $bcstr | bc)
      echo -e "$p\t$max\t$min\t$avg"
  done
  </code></pre>