Welcome!

Linux Authors: Michael Sheehan, Lavenya Dilip, Ian Thain, Bruce Armstrong, Ellen Rubin

Related Topics: Linux

Linux: Article

Formatting HTML in a Script

Making /etc/passwd Web-friendly

Last month, I explained how you can use the grep and cut tools to extract specific data fields from complex XML-based files, especially in the Safari web browser's bookmarks file on Mac OS X (yeah, I know, Mac OS X isn't Linux, but the two systems share a lot of common Unix underpinnings and, well, the Mac is a prettier laptop. Roll with me, the scripting environment is basically identical anyway).

This time I want to switch to the similar, though not identical, task of producing a Web-friendly version of the /etc/passwd file, showing you how you can extract data and translate it into specific HTML so that the data file can be transformed into an easily-shared Web page in just a few lines.

Extracting the Specific Data
One powerful capability of shell scripts is that you can change the field separator from a space to any other character you might need. Working with the password file, the obvious switch is to use a ":" as is obvious when you look at a representative line from this data file:

leah:*:1008:1008:leah999:/home/leah:/bin/sh

The fields here are account name, encrypted password (the "*" is a placeholder because the encrypted password is stored in a separate shadow file), user ID, group ID, optional comment field, home directory, and, finally, login shell.

The trick is to be able to tease out all this data in a shell script, and to do that, I'll change the internal field separator value, or IFS, to a colon, and then use a while loop in a way you might not have seen before:


pwf="etc.passwd"

IFS=":"

while read name pw uid gid comment home shell
do
echo "read in name = $name home = $home and shell = $shell"
done < $pwf

exit 0
Running this rather interesting-looking script produces:

$ ./login-webpage.sh | head -5
read in name = root home = /root and shell = /bin/csh
read in name = toor home = /root and shell =
read in name = daemon home = /root and shell = /sbin/nologin
read in name = operator home = / and shell = /sbin/nologin
read in name = bin home = / and shell = /sbin/nologin
Perfect.

Getting things HTMLized
Now, we need to take the nicely named data fields and turn them into some attractive HTML-based data. To do that, I'll pour them into a table in which the rows are denoted by <tr> and </tr> and the individual data cells by <td> and </td>. The script changes like this:


echo "<table border=1>"

while read name pw uid gid comment home shell
do
echo "<tr><td>$name</td><td>$uid</td>"
echo "<td>$comment</td><td>$home</td>"
echo "<td>$shell</td></tr>"
done < $pwf

echo "</table>"
Now you can see that once the fields are identified, it's pretty darn easy to change the echo statements to produce what we wanted. Note the addition of the <table> at the beginning and the </table> at the end of the loop.

This isn't really what I wanted, though, because I only wanted to see accounts with valid login shells, so it's time to add a conditional test in the while loop to see if the shell's legit:


while read name pw uid gid comment home shell
do
if [ "$shell" != "/sbin/nologin" ] ; then
echo "<tr><td>$name</td><td>$uid</td>"
echo "<td>$comment</td><td>$home</td>"
echo "<td>$shell</td></tr>"
fi
done < $pwf
Notice that you need to ensure that it's not the nologin shell rather than test to see if it's a legit login shell because accounts with no specified shell are given a default login shell of /bin/sh (that is, they're still valid login shells).

Putting It All Together
Put this all together and here's how we can use this script:

$ ./login-webpage.sh > account-info.html

and then the password file information can be viewed in a Web browser, as shown in Figure 1.

I'd also like to invite you, dear reader, to e-mail me with some ideas for shell scripts that you'd like to see me build and/or disassemble and explain too. Please send your ideas to me at d1taylor@gmail.com

More Stories By Dave Taylor

Dave Taylor, a contributing editor to Linux.SYS-CON.com, has been involved with the Linux and Unix community since 1980 and has written a number of best-selling Unix books. Currently, he writes, teaches, and works as a management consultant to tech startups, along with his new venture, Ask Dave Taylor!, www.askdavetaylor.com and his personal blog is www.blog.Linux.SYS-CON.com. To contact Dave, please go to /www.intuitive.com/contact.shtml.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.