Assigning * as value in a ksh array

I want to extract each and single character from a password string and put it in an array.

I tried this :

set -A password "echo $passwd | awk '{for (i=1; i<=length($1); i++) printf "%s ",substr($1,i,1)}'`

It's working as long that the password string doesn't contains any *

I tried a few thing but no luck. Anyone having an idea ?

There is no need to set it in an array. You can use the substring operator to get each individual character. It works like ${variablename:offset:length} so use 1 for length and give it a value of 0 through length for offset.

This also avoids needing to split on spaces, which allows people to put spaces, wildcards, and other special characters in their passwords without this choking.

N=2
pass="12345"
echo "${pass:$N:1}"

Thanks for your quick response but its not working on my system. I'm on AIX 7.1

ce9888@a03d:/users/ce9888 > N=2
ce9888@a03d:/users/ce9888 > pass="12345"
ce9888@a03d:/users/ce9888 > echo "${pass:$N:1}"
ksh: "${pass:$N:1}": 0403-011 The specified substitution is not valid for this command.
ce9888@a03d:/users/ce9888 >

You must be using ksh88, which is 27 years old and counting. This isn't going to be pretty.

Try

N=1
set -a pass
while [ "$N" -le "${#password}" ]
do
        pass[$N]="`echo \"$password\" | cut -b $N`"
        let N=N+1
done

In AIX (every AIX since 3.2 - the one i started with at the beginning of the nineties) the default login shell is a ksh88 version f. Fortunately there is a ksh93 installed per default (in every AIX since 4.something, ~2000), you just have to start it:

/usr/bin/ksh93

contains a ksh93 version t on AIX.

@Corona:

even ksh88 understands POSIX process substitution (" $(....) ")and can do variable manipulation:

x="ab*de"

while [ -n "$x" ] ; do
     first="${x%${x#?}}"
     x="${x#?}"
     print - "First: $first \tRest of x: $x"
done

I hope this helps.

bakunin

1 Like

That's a neat trick to get the first character without using substrings in a crummy shell. I'll remember that.