hotlinks0
hotlinks0

Reputation: 101

line 39: [: ==: unary operator expected

I am trying to generate a password with certain requirements. When I enter the while loop to generate a random character from the array it is fine until I add a count for my index "$i"

With the following code:

#!/bin/bash

#SET ARRAY VALUES
all=( 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z )

echo

#SET COUNT VALUES TO 0
numc=0
lowc=0
upc=0
i=0

while true;
do
#GENERATE PASSWORD
phrase[$i]=${all[$RANDOM%62]}
    #CHECK IF PASSWORD MEETS REQUIREMENTS
    for ((n=0; n<10; n++))
    do
            if [ ${phrase[$i]} == ${all[$n]} ]
            then
            echo num ${all[$n]}
            let "numc++"
            let "i++"
            fi
    done

I get the error "line 39: [: ==: unary operator expected"

but if I remove the let "i++" line then there is no error. But I need to increase my index in order to exit the loop and check the minimum length of the password

Upvotes: 2

Views: 1401

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81022

If ${phrase[$i]} (by the way you don't need $ in [$i] context ${phrase[i]} works too) is ever the empty string that if test will become [ == value-of-all-n ] which isn't a valid test.

Either quote the variables (which is almost always the right thing to do) or prevent that from ever being the empty string. (Was that i++ supposed to happen outside the inner loop?)

Upvotes: 3

Related Questions