Results 1 to 2 of 2
$ gawk -F':' '{ print $1}' /etc/passwd
The above command will display all the users.
I make an alias for this command,
$ alias lsusers="gawk -F':' '{ print $1}' /etc/passwd"
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-31-2013 #1Just Joined!
- Join Date
- Jan 2013
- Posts
- 1
alias to list all the users
$ gawk -F':' '{ print $1}' /etc/passwd
The above command will display all the users.
I make an alias for this command,
$ alias lsusers="gawk -F':' '{ print $1}' /etc/passwd"
The I use the alias to issue the command
$ lsusers
It is supposed to do be the same as
$ gawk -F':' '{ print $1}' /etc/passwd
But lsusers not just list users, it is the same as
cat /etc/passwd
What is wrong with this alias?
$ alias lsusers="gawk -F':' '{ print $1}' /etc/passwd"
- 01-31-2013 #2
Hi and welcome
The problems are quoting and (lack) of escaping.
If you call alias after defining your lsusers, you will see this mess:
As you can see, single quotes have been added and escaped. Which looks ugly.Code:$ alias alias lsusers='gawk -F'\'':'\'' '\''{ print }'\'' /etc/passwd'
But also the $1 didnt make it into the alias, as it has been already interpreted as empty by the shell.
So you could escape the $:
And this actually works.Code:$alias lsuers="gawk -F':' '{ print \$1}' /etc/passwd" $alias alias lsusers='gawk -F'\'':'\'' '\''{ print $1}'\'' /etc/passwd'
But this approach has issues:
- ugly quoting
- escapes needed
- hardcoded /etc/passwd
- limited to /etc/passwd
- required gawk, which may not be available
My suggestion is to use getent.
getent is part of the libc package, cut is in the core-utils package. Which means they *will* be available.
And getent will query the same user/group/network/etc backends (be it files or ldap or whatever) that the system itself uses.
So your alias would look like this:
Code:$alias lsusers='getent passwd | cut -d ":" -f1' $alias alias lsusers='getent passwd | cut -d ":" -f1'
Last edited by Irithori; 01-31-2013 at 09:48 AM.
You must always face the curtain with a bow.



