Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allowing awk to use shell variables

Status
Not open for further replies.
Nov 24, 2001
2
GB
I am trying to run a script which pulls out all users from the /etc/passwd file, subjects that list of users to exclusion criteria (removing system users from the list such as root, lp, sys etc) and produces a new list of users. I need to pass the new list of users to awk to determine the users home directories in the passwd file.

I then need to 'find' files in the users home directories which they own. This all sounds complicated because there are in-consistencies on the file system which deems this necessary.

So far I have set up variables:
############
CRITERIA='(^root|^bin|^sys|^lp|^uucp|^nuucp|^listen|^noaccess|^daemon)' # this sets out the list of not-needed users

USERS=`nawk -F: '$1 !~ /${CRITERIA}/ {print $1}' /etc/passwd` # this produces the new list variable
############

#The following attempts to pull out the home directories and search in those directories for files

############
for people in $USERS
do

HOME=`grep "^$people" /etc/passwd |nawk -F: ' {print $6}'`

find $HOME -user $people -exec ls -l {} \; | awk '$3 !~ /^root/ {print}'
done
############

My 'excluding root,lp etc users from my list is not recognised in awk - the original list of users is passed unaltered to the find command and all users have their home directories interogated.

What is my syntactical error?
 
passing variables to awk has been looked at before with the -v, but I have a list of variables to set up.......do I need to enter -v ... -v ... -v ... etc etc a mile long?
 
This has saved me much time.

for xx in $(var_list)
do

awk -v inX=$xx ' {
script
}'
done
 
nawk -F: '$1 !~ /${CRITERIA}/ {print $1}' /etc/passwd

The shell doesn't subtitute the variable because ${CRITERIA} is in a single quote delimited string.
So nawk selects lines with first word ($1) begining with end of line ($), in other words nawk selects all not empty lines.


Consult faq271-1281 "Shell variables in awk"

If you don't want to use the -v option, you can access to exported shell variables with the ENVIRON array :

export VAR1='the value...'
awk ' { print "The value of VAR1 is " ENVIRON["VAR1"] ; ... } ' input_file


A possible solution to your problem :

export CRITERIA=^root|^bin|^sys|^lp|^uucp|^nuucp|^listen|^noaccess|^daemon)'
awk -F: -f UsersFiles.awk /etc/passwd

with UsersFiles.awk :


$1 !~ ENVIRON["CRITERIA"] {
User = $1 ;
Home = $6 ;
FindCmde = "find \"" Home "\" -user \"" User "\" -exec ls -ld {} \\;" ;
while ( FindCmde | getline ) {
if ( $3 != "root" )
print $0 ;
}
close(FindCmde) ;
}

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top