Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Discuss] inventory of most-used programs; bash quoting problem



On Fri, May 31, 2013 at 11:08:13AM -0400, Greg Rundlett (freephile) wrote:
> I was trying to do the following to report on the most used commands (for
> each user found in /home).
> 
> find /home -maxdepth 2 -name '.bash_history' -exec bash -c 'echo -e
> "\n$1\n"; cat "$1" | awk '{ print $1 }' | sort | uniq -c | sort -rn | head
> ' _ '{}' \;
> 
> But I couldn't figure out how to get awk quoting to work inside the bash -c
> string literal.  I looked it up, so now I know how to do it. I know it's
> been discussed on this list before.  I just throw it out there as a
> refresher :-)
> 
> Answer: Use these four characters '\'' to represent a single quote (inside
> a single quoted string)

Note that this works because it closes the current single-quoted
string, quotes the next quote so it is treated as a literal instead of
interpreted by the shell, and then re-opens the quoted string.

Alternatively, you can use a double-quoted string, like this (note the
quoting of double quotes and dollar signs):

find /home -maxdepth 2 -name .bash_history -exec bash -c "echo -e \"\n\$1\n\"; cat \"\$1\" | awk '{ print \$1 }' | sort | uniq -c | sort -rn | head " _ {} \;

This is more similar to how you would do it in other languages, and in
my experience is the more common way to write it in shell.  Note that
the quotes around .bash_history and the {} in your original command
are superfluous.  They don't hurt anything, but in commands which
otherwise already have a lot of quoting, they may serve to make the
quoting more difficult to parse by a human.  I would favor leaving
them out. :)


-- 
Derek D. Martin    http://www.pizzashack.org/   GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address.  Replying to it will result in
undeliverable mail due to spam prevention.  Sorry for the inconvenience.




BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org