1

So I'm trying to create a script to generate a random password with certain criteria, but for some reason some of my integer values aren't showing as such. Here is the script:

#!/bin/bash
PASSWORDLENGTH= shuf -i 8-16 -n 1
RNDSOURCE=/dev/urandom
L="acdefghjklmnopqrstuvwxyz"
U="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
N="012345679"
S='@#$%&*+-='
ALL="$L$U$N"
function rndindex() { echo $(($RANDOM % ${#1})); }
password="${L:$(rndindex $L):1}${U:$(rndindex $U):1}${N:$(rndindex $N):1}${S:$(rndindex $S):1}"

password=$password${ALL:$(rndindex $S):1}
while [ "${#password}" -lt "$PASSWORDLENGTH"" ]   #Line 15
do
  password=$password${ALL:$(rndindex $ALL):1}
done

chars=$password
password=""
while [ "${#password}" -lt "$PASSWORDLENGTH"" ]   #Line 22
do
  n=$(rndindex $chars)
  ch=${chars:$n:1}
  password="$password$ch"
  if [ $n = $(( ${#chars} - 1 )) ]; then
      chars="${chars:0:$n}"
  elif [ $n = 0 ]; then
      chars="${chars:1}"
  else
      chars="${chars:0:$n}${chars:$((n+1))}"
  fi
done
echo $password

The error messages I'm getting are:

line 15: [: : integer expression expected

line 22: [: : integer expression expected

2

Your 2nd line is wrong. It should be:

PASSWORDLENGTH=$(shuf -i 8-16 -n 1)

and shrink double quotes "" to one " in line 15 & 22.

ps.: use pwgen or apg, don't reinvent the wheel.

| improve this answer | |
  • I cannot use pwgen or apg for the purpose of the assignment because the distribution it will be tested on does not come with either by default, and my proff isn't willing to yum them onto it. Thanks for the reply! – DJatomica Oct 30 '16 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.