I am having difficulty splitting a colon-delimited string and assigning the
tokens to an array in a bash shell script. I want to assign the passwd fields
returned by `id -P username` to an array.
Given that
`id -P cyrus`
returns
cyrus:*:77:6::0:0:Cyrus User:/var/imap:/usr/bin/fa
lse
I wish to assign the fields to an array, say pw, such that
pw[0] = cyrus
pw[1] = *
pw[2] = 77
...
pw[8] = Cyrus User
pw[9] = /var/imap
... etc
I already discovered that ' set -f ' avoids the '*' from getting filename-expanded.
I tried using awk to split the colon-delimited string, but the GECOS field can contain spaces,
resulting in its value getting further split during the array assignment:
...
pw[8] = Cyrus
pw[9] = User
....
I have a feeling that there is a simple solution, but I haven't found it yet.
Equally acceptable would be a 'read'- style assignment to multiple variables.
Something like
IFS=':'
read f_uname f_pass f_uid f_gid ...
But read takes intput from stdin.
Start Free Trial