apg as bash function

# Generate a random password
#  $1 = number of chars, defaults to 12
function apg() {
  if [ "$1" = "" ]
  then
      len=8
  else
      len=`expr $1 / 2 + 1`
  fi
  # half of chars with special chars
  #     get random bits   | encode     | take line 2  | get char 2->
  var1=`head /dev/urandom | uuencode - | sed -n 2p    | cut -c2-$len`
  # rest of chars with base64
  #     get random bits   | encode base64 | take line 2 | get char 2->
  var2=`head /dev/urandom | uuencode -m - | sed -n 2p   | cut -c2-$len`
  echo "$var1$var2"
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>