简体   繁体   中英

Linux script help needed

I am trying to create a script that will use the /etc/passwd file to create a listing like this:

Full Name: Blah Blah User Account: bblah User ID: 5000 Last logged in: Sun Feb 21 18:13 Full Name: Mike Mike User Account: mmike User ID: 5001 Last logged in: Sun Feb 21 18:13

With my script, I get it to show Full Name: Blah Blah User Account: bblah User ID: 1000 because I grep the 1000 accounts in my passwd file. As soon as I do the 5000 series, I get all the full names, then all the user accounts, and then all the user ids listed. How do I go about fixing this? How do I go about adding the "Last logged in" bit?

Is there a way to do this without using awk?

Script so far is:

#!/bin/bash
passfile=/etc/passwd

for i in  $(grep 5000 ${passfile} | cut -d : -f 5)
do


        account=$(grep 5000 /etc/passwd | cut -d : -f 3)
        username=$(grep 5000 /etc/passwd | cut -d : -f 1)
        echo Full Name:${i} Username: ${username} User ID: ${account} Last login: ${lastlogin)
done

上次登录是由最后一个命令完成的:

last username

Use this:

#!/bin/bash

echo -en "BEGIN{\n}{\n    system(var)\n}\n" > h.awk
while read in; \
 do \
  uname=`echo $in | sed -r 's/:/\ /g' | awk '{print $1}'`; \
  p1=`echo $in | sed -r 's/:/\ /g' | awk '{print "name: "$5, "username: "$1, "id: "$3}'`; \
  p2=`echo | awk  -f ./h.awk var="last $uname| head -n 1"|awk '{print $5 $6}'`; \
  echo $p1 last login: $p2; \
done < /etc/passwd

If you need more detail of last login to be prompted, use this one:

echo -en "BEGIN{\n}{\n    system(var)\n}\n" > h.awk
while read in; \
 do \
  uname=`echo $in | sed -r 's/:/\ /g' | awk '{print $1}'`; \
  p1=`echo $in | sed -r 's/:/\ /g' | awk '{print "name: "$5, "username: "$1, "id: "$3}'`; \
  p2=`echo | awk  -f ./h.awk var="last $uname| head -n 1"|awk '{print $4" "$5 $6" "$7}'`; \
  echo $p1 last login: $p2; \
done < /etc/passwd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM