sed from a shell script - invalid command code

Jez Hancock jez.hancock at munk.nu
Wed Dec 10 06:41:04 PST 2003


I'm trying to get the following Bourne shell script to output a list of
all users on the server with the exception of those listed in the
$ignore_users variable:

-snip-
#!/bin/sh
sed=/usr/bin/sed
passwd_file=/etc/passwd

ignore_users="root|toor|daemon|operator|bin|tty|kmem|games|news|man|smmsp|bind|uucp|xten|pop|nobody|mysql|www|sshd|ftp|cyrus"

cmd="$sed -E -e '/^(#|$ignore_users)/d' -e 's/:.*//' $passwd_file"

users=`cmd`
echo $users
-snip-

However when I run this code it returns:

sed: 1: "'/^(#|root|toor|daemon| ...": invalid command code '

How can I get this code to work?

If I echo out the contents of $cmd it looks as it should do:

/usr/bin/sed -E -e '/^(#|root|toor|daemon|operator|bin|tty|kmem|games|news|man|smmsp|bind|uucp|xten|pop|nobody|mysql|www|sshd|ftp|cyrus)/d' -e 's/:.*//' /etc/passwd

and if I execute that command I get the desired result - I'm just having
trouble getting the results of the command into a variable.

TIA

-- 
Jez Hancock
 - System Administrator / PHP Developer

http://munk.nu/
http://jez.hancock-family.com/


More information about the freebsd-questions mailing list