Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i am trying to select only username with uid > 500 in /etc/passwd and extract mindays, maxdays from /etc/shadow. can anyone help? thanks!

IFS=":" ; export IFS
cat /etc/passwd | while read username passwd_placeholder userid groupid capacity gecos home_dir shell; do
if [ $userid -gt 500 ] ; then
echo $username
shadow_line=`grep "^$username:" /etc/shadow`
echo "###$shadow_line###"
passwd=`echo $shadow_line | awk '{ print $2 }'`
lastchg=`echo $shadow_line | awk '{ print $3 }'`
min=`echo $shadow_line | awk '{ print $4 }'`
max=`echo $shadow_line | awk '{ print $5 }'`
warn=`echo $shadow_line | awk '{ print $6 }'`
inactive=`echo $shadow_line | awk '{ print $7 }'`
expire=`echo $shadow_line | awk '{ print $8 }'`
flag=`echo $shadow_line | awk '{ print $9 }'`
echo "Userid $passwd $lastchg $min $max $warn $inactive $expire $flag"
fi
done

hi david,
im using bourne shell. what does the following lines mean?
passwd_placeholder userid groupid capacity gecos
home_dir shell; dothanks!

hi david,
can u explain why the following doesnt work:
#!/bin/sh
TEMPFILE1=/tmp/ehong.1.$$
IFS=":" ; export IFS
cat /cygdrive/h/etc/passwd | while read username passwd_placeholder userid groupid gecos home_dir shell; do
if [ $userid -gt 500 ] ; then
echo $username
shadow_line=`grep "^$username:" /cygdrive/h/etc/shadow``echo $shadow_line | awk -F: '{ print $1":"$3":"$4":"$5":"$6 }' /cygdrive/h/etc/shadow` > $TEMPFILE1
fi
donei have been figuring out abt this for a week. i have problem extracting info from /etc/shadow with username input from /etc/passwd & output to a temp file for comparing data later on. help is appreciated! thanks!

You would surround a command with back ticks when you want to assign the output to a variable such as
var=`date +%Y%m%d`
You don't want them when you want the output written to a file as in your echo line being redirected to a tempfile above.

i just realise what i am doing might be wrong. my question is as follows:
###go through all HUMAN user password entries (you may assume that any user with a uid above 500 is a human user for the purposes of this assignment) and ensure that they have the following passwd and shadow setting set:
Minimum days to change password is 7
Maximum life of a password is 30 days, and the users will be asked to change their password for 5 days up to the end of the time their password is valid.
Ensure that the "last changed" setting for each user occurs in the past. Report any that do not match this (include the date set in the shadow file in the report)###what i am doing above is extract data to a temp file. how do i update the correct settings later on?

no, im running this script as root. i have to ensure all affected users (i.e.>500) have the correct settings in the shadow file. any suggestion?

not an easy answer but you could alter the fields with sed to create a new file with the same permissions then replace the working /etc/shadow with the new one
cp -p /etc/shadow /etc/shadow.working
sed -e 's/^$username:.*:/$username:somevalue:/ /etc/shadow > /etc/shadow.working && mv /etc/shadow.working /etc/shadow

I've got one query of the problem that is mentioned by uribo that is ...
"Ensure that the "last changed" setting for each user occurs in the past. Report any that do not match this (include the date set in the shadow file in the report."
How it can be done by bourne shell ?

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |