bash bash bash
August 8, 2006 1:03 PM   Subscribe

Calling all bash/nix gurus.. please help me!

I have a list of users that I'm trying to parse and convert into a html-friendly format, surrounding them around <option> tags so I can put them in an HTML select form.

I'm getting input from /etc/passwd, grepping to find users that are enabled login, cutting and getting the first segment before a colon, and sorting in alphabetical order -- then surrounding the resulting list of login IDs in option tags:

cat /etc/passwd | egrep "\[N?U" | cut -d : -f 1 | sort | sed -n 's/.*/<option value=\"&\">&<\/option>

Which yields:
<option value="loginID1"\>loginID1</option\>

That's all fine, not a problem. But -- now I want to run each ID through 'lookup' and grep the name out, and then insert between the option tags, where the second ampersand is, like this:

<option value="loginID1">Firstname Lastname1</option>

Assuming that lookup loginID1 returns Firstname Lastname 1, how can I do this? It seems that I need to keep the original values of the /etc/password - parsed values while also running them through lookup. Is there any way I can do this in one line, without having to resort to loops/arrays in bash? Or will an in-line Perl script help?

posted by provolot to Computers & Internet (9 answers total)
Since you need to run 'lookup' once per line, you'll need some sort of loop, whether it's in bash or in a script. Here's a bash one-liner:

for i in `egrep "\[N?U" /etc/password | cut -d: -f1 | sort`; do echo <option value=\"$i\">`lookup $i`; done
posted by mbrubeck at 1:20 PM on August 8, 2006

My </option> was cut off because I forgot to escape the HTML.
posted by mbrubeck at 1:21 PM on August 8, 2006

for a in `cut /etc/passwd -d: -f 1,5`
echo ""
posted by RustyBrooks at 1:22 PM on August 8, 2006

yeah, let me try that again

for a in `cut /etc/passwd -d: -f 1,5`
echo "<option value=`echo $a | cut -d: -f1`>`echo $a | cut -d: -f2`</option>"
posted by RustyBrooks at 1:23 PM on August 8, 2006

(note, no lookup required, just process it as you go through the password file. You'll need to do some work on what happens if there's no user name, or if there's a blank or commented line in the password file, etc. There's no way I'd do this in bash, I'd use perl or something)
posted by RustyBrooks at 1:24 PM on August 8, 2006

Oh, and stick your egrep stuff back in. I took it out for clarity when I was fooling around with this.
posted by RustyBrooks at 1:26 PM on August 8, 2006

Thanks guys, this worked. I would do it in perl, except it's part of a larger script...
posted by provolot at 1:57 PM on August 8, 2006

And, if you're building a CGI script in #!sh, *be careful*.

I've done this, but it's really not recommended.
posted by baylink at 2:04 PM on August 8, 2006

Ok; as long as you have perl to sanitize your arguments/data... NP
posted by baylink at 5:55 PM on August 8, 2006

« Older When should I buy a car?   |   Remote Control Dell Optiplex Hack Newer »
This thread is closed to new comments.