Skip to content

loading multiple variables in ksh

A few days ago, I had to re-populate home directories after an alt_disk installation from an earlier prepared mksysb image which was created with an exclude list containing /home. This action resulted in a post documenting the recreation process based on a few lines of ksh code.
There are many other options, some does not need scripting at all. A previous backup or scp from the source host could be used to receive the same result.

The next snippet shows the way to simultaneously load (read) multiple (in this case two) variables (user and home) in ksh.

Please notice, that egrep is employed to exclude system accounts as here the root and the rest of system accounts have their homes either outside of /home or not at all.

cat /etc/passwd | egrep -v \ "^daemon|^nobody|^lpd|^root|^adm|^bin|^sys|^uucp|^lp|^snapp" |\
awk -F ':' '{print $1 $6}' | while read user home
         pgrp=`lsuser -a pgrp $user | awk -F '=' '{print $2}'`
         mkdir -p $home
         chown $user.$pgrp $home

Awk is used to extract the first and the sixth element from each line of the /etc/passwd which are next used to initialize the two variables – user and home. The first variable is used to obtain user’s primary group, the second to create his/hers home directory which ownership is set using all three variables. Notice usage of -F to change awk's internal Input Field Separator variable (FS) – in /etc/passwd it is : character but to access user’s primary group it has to be changed to the = character.

To learn more about awk you may follow this link – to the Grymoire Blog.

Posted in Real life AIX.

Tagged with , , , , , , .

2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Eliot Wilson says

    looks like a pipe symbol is missing just before the “while” in the code snippet

  2. MarkD:-) says

    Nothing beats another pair of eyes – thanks Eliot!!!!!

Some HTML is OK

or, reply to this post via trackback.

WordPress Anti Spam by WP-SpamShield

Copyright © 2016 - 2017 Waldemar Mark Duszyk. All Rights Reserved. Created by Blog Copyright.