LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2010, 11:35 AM   #1
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
How to 'watch' a command including both of single and double quote?


Hi guys,

I want to watch a number of processes in "D" status repeatedly with following command:
Code:
# watch -n 1 'top -b -n 1 | awk '{if ($8 == "D") {print; count++} } END {print count}''
but it didn't work. I also tried with double quote.

Can I use 'watch' command is this case?

PS: I know 'while' and 'sleep' can do the same but it is dirty workaround.
 
Old 08-12-2010, 11:45 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
1. You can't do quotes like that. Either escape them or stagger them. ' then " then ', then close them in reverse order. You have ' then ' then " and it sees 'top -b -n 1 | awk ' as one unit.
2. $8 needs to be escaped (\$8) so that the shell doesn't parse it.

Code:
watch -n 1 "top -b -n 1 | awk '{if (\$8 == "D") {print; count++} } END {print count}'"
works without error, though it prints just "2" for me and I don't know why.
 
Old 08-12-2010, 02:29 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You also have to escape the double-quotes inside the awk command.
Code:
watch -n 1 "top -b -n 1 | awk '{if (\$8 == \"D\") {print; count++} } END {print count}'"
Basically, single-quotes escape everything, while double-quotes escape everything inside them except for $, `, and \, and of course another ", which ends the sequence. This means that inside double-quotes you can use \ to escape anything that could be a problem, including other double-quotes. But inside single-quotes you can't use anything, not even the escape character.

Note also that double-quotes lose their special meaning inside single quotes, and vice-versa.

So in this case, you have to use double-quotes around the phrase as a whole, which allows you to use escapes, then you need to escape any [$`\"] characters that are intended to be fed to the commands inside. In this case, you need to protect the $ and " inside the awk expression, because the single-quotes surrounding them have lost their special bash meaning.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed : Convert double quote to singe quote pgb_710 Programming 6 01-21-2010 07:59 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
gvim - double quote char always replaced with two single quotes in special modes JazzItSelf Linux - Software 4 09-16-2008 11:26 AM
minicom won't send a double quote character kscott121 Linux - Software 0 05-22-2008 02:34 PM
Double and single quote, Question in expect evilchild Programming 4 08-22-2004 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration