LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 12:35 PM   #1
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
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, 12:45 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,817

Rep: Reputation: 609Reputation: 609Reputation: 609Reputation: 609Reputation: 609Reputation: 609
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, 03:29 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 08:59 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
gvim - double quote char always replaced with two single quotes in special modes JazzItSelf Linux - Software 4 09-16-2008 12:26 PM
minicom won't send a double quote character kscott121 Linux - Software 0 05-22-2008 03:34 PM
Double and single quote, Question in expect evilchild Programming 4 08-22-2004 06:34 PM


All times are GMT -5. The time now is 12:53 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration