Find the answer to your Linux question:
Results 1 to 6 of 6
Hi to all.I have this script: #!/bin/bash awk -F: '{print $1 "\t" $6}'</etc/passwd and I want to modify it so that it will print only the login names and the ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25

    Need help with a script..

    Hi to all.I have this script:
    #!/bin/bash
    awk -F: '{print $1 "\t" $6}'</etc/passwd
    and I want to modify it so that it will print only the login names and the home directories of the users that are currently logged in the system.
    Thanks in advance..

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    You might want to combine this with the who command. The who command will tell you who is online now. So something that would parse the logged on users then check them against /etc/passwd
    Code:
    for loggedin in $(who |awk '{print $1}'
    do grep $loggedin /etc/passwd |awk -F:'{print $1 "\t"$6}'
    done
    That's just rough syntax, you will need to test that yourself. The logged in variable is set as the first token from each line returned by the who command, i.e. the username. This is then grep'd in /etc/passwd and your awk filter is then applied.

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    Yeap.That's exactly what I need.But now all I need is help with the syntax.
    Please..

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    just awk
    Code:
    awk -F":" 'BEGIN{ "who -q" | getline line}
    { user[$1]=$6 }
    END {
      n=split(line,l," ")
      for(p=1; p<=n; p++ ) {
       print l[p],user[l[p]]
      }
    }' /etc/passwd

  5. #5
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    Thanks my friend.It works like a charm.It is exactly what I need.

  6. #6
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    Quote Originally Posted by ghostdog74 View Post
    just awk
    Code:
    awk -F":" 'BEGIN{ "who -q" | getline line}
    { user[$1]=$6 }
    END {
      n=split(line,l," ")
      for(p=1; p<=n; p++ ) {
       print l[p],user[l[p]]
      }
    }' /etc/passwd
    That's tidy ghostdog. I think I'll need to read up more on awk!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...