1753834 Members
7888 Online
108806 Solutions
New Discussion юеВ

scripting user list

 
SOLVED
Go to solution
MSwift
Regular Advisor

scripting user list

Here is what i trying to do. First, i am trying to generate a userlist file (where i want to (logins -u) and capture only the id's which are names (we have most id's which are in the format of (2 alphabets and 4 numbers) (ex, jk4567) and we want to exclude those from the userlist file, so that we have the other id's. once the userlist file is generated, then we want to read that file and run a command $USERID so that it modifies the attributes for each id (which are NOT in jk4567) format. Could someone help with this Please?

Thanks

Mike
19 REPLIES 19
Dennis Handly
Acclaimed Contributor

Re: scripting user list

Just use grep:
grep -v '^[a-zA-Z]\{2\}[0-9]\{4\}' user-list
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi Mike:

One way to generate your file of ids is this way:

# perl -e 'open($fh,"-|","logins -u") or die;while (<$fh>) {@F=split;print unless $F[0]=~m{\d+}}' > /var/tmp/logins

Then you can do:

#!/usr/bin/sh
while read USERID X
do
echo ${USERID}
done < /var/tmp/logins

You could imbed the Perl snippet in your shell script too.

Regards!

...JRF...
MSwift
Regular Advisor

Re: scripting user list

JRF:

How would put all this in the same shell? Also is there a way the whole thing could be done via a shell script alone?

Thanks

Mike
Dennis Handly
Acclaimed Contributor

Re: scripting user list

>is there a way the whole thing could be done via a shell script alone?

You don't want to use my grep or JRF's perl solution?

You can also use a real shell's pattern matching:
if [[ name != [a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9] ]]; then
Dennis Handly
Acclaimed Contributor

Re: scripting user list

Oops, that should have been:
if [[ "$name" != [a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9] ]]; then
James R. Ferguson
Acclaimed Contributor
Solution

Re: scripting user list

Hi (again) Mike:

> How would put all this in the same shell? Also is there a way the whole thing could be done via a shell script alone?

#!/usr/bin/sh
#!/usr/bin/sh
set -u
MYFILE=/var/tmp/mylogins
trap 'rm ${MYFILE}' EXIT
perl -e 'open($fh,"-|","logins -u") or die;
while (<$fh>) {@F=split;print unless $F[0]=~m{\d+}}' > ${MYFILE}
while read USERID X
do
echo ${USERID}
done < ${MYFILE}

...in pure Perl, I might do:

#!/usr/bin/perl
use strict;
use warnings;
my @F;
my $file = '/var/tmp/mylogins';
open(my $fh, "-|", "logins -u") or die "Can't open $file:$!\n";
while (<$fh>) {
@F=split;
system( "echo", $F[0] ) unless $F[0]=~m{\d+};
}
1;

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi Mike:

Here's a pure shell variation along the lines that Dennis began:

#!/usr/bin/sh
logins -u | while read USERID X
do
echo ${USERID} | grep -q -E '^[a-zA-Z]+$' && echo ${USERID}
done

Regards!

...JRF...
MSwift
Regular Advisor

Re: scripting user list

JRF:

One more question. in the script above i see 2 echo statements, i plug in the actual command instead of the second echo statement- correct?

Thanks

Mike.
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi (again) Mike:

> One more question. in the script above i see 2 echo statements, i plug in the actual command instead of the second echo statement- correct?

Yes.

Given:

# echo ${USERID} | grep -q -E '^[a-zA-Z]+$' && echo ${USERID}

...we are feeding the ${USERID} value to 'grep' with the first 'echo'. If the return value from 'grep' is zero, there was a match and the second statement (another 'echo')is run. Hence, this is the one where you want to substitute the real command.

Regards!

...JRF...