Help regarding shell script

Mike what OS are you using? What does this command give:

grep -E "^passwd|^group" /etc/nsswitch.conf

Do you have getent?

which getent

hi,

Im using AIX.

when i type the code it says:

grep: 0652-033 Cannot open /etc/nsswitch.conf.

and for

$which getent
 
which: 0652-141 There is no getent in /pgms/scheduler/lsf/7.0/aix5-64/etc //pgms/scheduler/lsf/7.0/aix5-64/bin /usr/bin /etc /usr/sbin /usr/ucb /usr/bin/X11 /sbin /usr/java5/jre/bin /usr/java5/bin.

Thanks

ok just to help this along I have not been keeping up but it looks like you are using LDAP for auth. so if you want a list of the groups try a lsgroup ALL
and put it in a file for reference and search it for group ID and match to human name.

Hi aix-guy,

I can link it up but how do i put in code to reflect the complete groupname displayed?

Thanks

ok not sure I understand what you are asking but here is a output from lsgroup that should be usable for you
lsgroup -c -a id ALL |grep -v "#"
It produces the following

system:0
staff:1
sys:3
uucp:5
mail:6
security:7
printq:9
ecs:28
nobody:4294967294
usr:99
perf:20
shutdown:21
haemrm:201
dba:219

Now the reflect the complete group name I am not understanding what you want? the first column is the group name the second is the group id.

Now if you want is shown all the time like from a ls command then:

  1. check if the max_logname is big enough
    lsattr -El sys0 -a max_logname
    getconf LOGIN_NAME_MAX
  1. if it is not big enough set it (up to 256 max):
chdev -l sys0 -a max_logname=20 <what you need>
  1. now for this to be effective you will have to reboot.
  2. once rebooted the all that is needed is a ls -lX (ls -(L) lower case and a uppercase X

---------- Post updated at 01:37 PM ---------- Previous update was at 01:27 PM ----------

Just to add if you want a output format with columns of consistent size then typeset is what you will need and the -R or -L (flush left or right) and the number of characters to show or fill: typeset -R20 Your_Var_Name
then when a value is assigned it will flush (L or R) and fill as needed and then you just display it.

I do not have an AIX host handy at the moment, but see if this works:

ls -n | 
awk -F: 'BEGIN{
           while("lsuser -c -a id ALL"  | getline) U[$2]=$1
           while("lsgroup -c -a id ALL" | getline) G[$2]=$1
         }
         { sub($3" "$4, U[$3]" "G[$4])} 1' FS=" "

I used the commands that aix-guy provided...

Hi Scrutinizer,

I couldn't run the script which you gave. It is incomplete I believe. And can you please test that for me and let me know about that?

Appreciate!!

Mike

Try this:

ls -n |
awk -F: 'BEGIN{
           while("lsuser -c -a id ALL"  | getline) U[$2]=$1
           while("lsgroup -c -a id ALL" | getline) G[$2]=$1
         }
         { sub($3" +"$4, U[$3]" "G[$4])} 1' FS=" "

I tested it on AIX 5.3

Hi Scrutinizer,

Thanks a lot for the script. It worked. But can you please run me through the script?

Thanks again,
Mike

Glad to see you finally got a command to fetch the user and group names.

Here my version updated to use the new commands (this will also get your columns lined up):

ls -n $@ | awk -F: '
function max(a,b) {return a>b?a:b}
function toend(str,fld)
{ for(i=0; i<length(str); i++) {
    while(substr(str,i,1) != " " && i<length(str)) i++
    while(substr(str,i,1) == " " && i<length(str)) i++
    if (!--fld) return substr(str,i)
  }
  return ""
}
BEGIN {
    while("lsuser -c -a id ALL"  | getline) {
        U[$2]=$1; UW=max(UW,length($1))}
    while("lsgroup -c -a id ALL" | getline) {
        G[$2]=$1; GW=max(GW,length($1))}
}
{ printf "%10s %3s %-*s %-*s %11s %s\n", $1, $2, UW, U[$3], GW, G[$4], $5, toend($0,5)
}' FS=" "

e.g

 $ lsnew .
-rw-r--r--   1 root         system           168 Jul 23 2010  asset.d
-rwxrwxrwx   1 chubler      chubler        71314 Mar 18 2010  assetupload.csv
drwxrwxr-x   5 chubler      chubler          256 Oct 28 12:27 bin
drwxrwxr-x   5 chubler      chubler          256 Oct 31 2009  env
drwxrwxr-x   2 chubler      chubler        12288 Feb 07 06:55 images
-rw-r--r--   1 root         system       8492187 Jul 23 2010  test.dump

Hi Chubler,

Loads of thanks regarding this issue from the start... Appreciate it!!!

The script works fine. But based on your example where you say the UID root, chubler (UID column) doesn't gets displayed though when I run the script. Does it matter ? And can you run through this

BEGIN {
    while("lsuser -c -a id ALL"  | getline) {
        U[$2]=$1; UW=max(UW,length($1))}
    while("lsgroup -c -a id ALL" | getline) {
        G[$2]=$1; GW=max(GW,length($1))}
}


Thanks
Mike

Thanks for the explanation S.

Do I have to tweak the code little bit in the substitute field 3 and 4 to have the UID displayed? (The UID is the third column which gets removed but the long group name gets displayed :slight_smile: )

If you want the UID (the number, $3) displayed as well you can use:

{sub($3" +"$4, U[$3]" "$3" "G[$4])}

Mike shoulds like your lsuser command isn't listing usernames and IDs.

from reading the code lsuser should be giving you output something like this:

$ lsuser -c -a ALL
root:0
daemon:1
bin:2
sys:3
adm:4
...

I don't really understand the problem if there is one? Mike said the code worked and I tested it on AIX and it seemed to be working fine...

I'm just going from Mike's statements:

I suspect that the lsuser command is only listing local user ID's and not AD users.

This version of the script might be safer (if UID or GID not found in array then then just diplay the ID instead of blanks).

ls -n $@ | awk -F: '
function max(a,b) {return a>b?a:b}
function toend(str,fld)
{ for(i=0; i<length(str); i++) {
    while(substr(str,i,1) != " " && i<length(str)) i++
    while(substr(str,i,1) == " " && i<length(str)) i++
    if (!--fld) return substr(str,i)
  }
  return ""
}
BEGIN {
    while("lsuser -c -a id ALL"  | getline) {
        U[$2]=$1; UW=max(UW,length($1))}
    while("lsgroup -c -a id ALL" | getline) {
        G[$2]=$1; GW=max(GW,length($1))}
}
{
    printf "%10s %3s %-*s %-*s %11s %s\n", $1, $2,
      UW, $3 in U?U[$3]:$3,
      GW, $4 in G?G[$4]:$4,
      $5, toend($0,5)
}' FS=" "

Hi S,

Your code worked fine after the tweak. The UID is just displaying in the numeric format but apart from that it is completely fine. Is there anything can be added to the code to make "1750560623" to display the userid "MIKE045" ??

Chubler,

The code S tweaked worked fine. and the following is the output I get as required. And I ran your code too I get the output as displayed below with only the numeric stuff displaying instead of UID MIKE045.. Can that be replaced??

-rwxrwxr-x    1 1750560623 Func-Wireless-consumers  10 Jan 27 12:21 list.sh
-rwxr-x---    1 1750560623 Func-Wireless-consumers  80 Jan 28 09:25 .desk

Mike, what do:

lsuser -c -a id ALL

and

lsgroup -c -a id ALL

produce?

Can you give an impression? Are all your long names present in the listing?

Hi S,

When I do for the lsuser I get in the following format :

#name : id
  mike001: 74861 

and so on for all users and for lsgroup

#name:id
Func-Wireless-Consumers :1399683538 and so on for all the groups

So the group all gets displayed when I run but the user displayed as 74861 instead of mike001.

Thanks
Mike

OK, so there are spaces. try this:

ls -n | 
awk -F: 'BEGIN{
           while("lsuser -c -a id ALL"  | getline){gsub(/ /,x);U[$2]=$1}
           while("lsgroup -c -a id ALL" | getline){gsub(/ /,x);G[$2]=$1}
         }
         { sub($3" +"$4, U[$3]" "G[$4])} 1' FS=" "