| By Dave Taylor | Article Rating: |
|
| June 19, 2005 11:00 AM EDT | Reads: |
8,017 |
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:
Running this rather interesting-looking script produces:
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
$ ./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
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:
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.
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>"
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:
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).
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
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
Published June 19, 2005 Reads 8,017
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Linux Virtualization and Tired Open Source Myths
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- Amazon Kindle Fire Gets Its Own 'Personal Cloud Desktop' with AlwaysOnPC App Launch
- SPIRIT DSP Receives 2011 INTERNET TELEPHONY Product of the Year Award
- Hadoop Quickstart: Use Whirr to automate standup of your distributed cluster on Rackspace
- Jury Gets Novell Antitrust Case Against Microsoft
- The Utility Infrastructure Security Market 2012-2022: Cybersecurity & Smart Grids
- FORTUNE Magazine Names Rackspace Among “100 Best Companies to Work For”
- iFollowOffice Turns to Virtual Bridges and Savvis for On-Demand Virtual Desktop Services
- EnterpriseDB Announces Availability of Postgres Plus Cloud Database
- i-Technology in 2012: Five Industry Predictions
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Amazon to Rent Out Supercomputers
- Amazon Émigré Starts Network Monitoring Firm
- HP’s Putting a Back Door in the Itanium Alamo
- Linux Virtualization and Tired Open Source Myths
- CloudLinux Announces Preferred Partner Program
- MapR Pushes the Hadoop Envelope
- Rightware Announces Gaming Performance Benchmark for OpenGL ES 3.0/Halti
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- 3Dconnexion Announces its Newest 3D Mouse - the SpaceMouse Pro
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- A Closer Look at Damn Small Linux
- Linus' Top Ten SCO Barbs
- SCO CEO Posts Open Letter to the Open Source Community
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Where Are RIA Technologies Headed in 2008?
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- Why Recovering a Deleted Ext3 File Is Difficult . . .


















