LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2010, 05:28 PM   #1
fabienhenon
LQ Newbie
 
Registered: Mar 2010
Posts: 3

Rep: Reputation: 0
double-quotes do not escape properly (bash=python)


Hi,

I wrote the Automatik widget (you can find it at :
http://kde-look.org/content/show.php...&PHPSESSID=cae
To improve it, I would like to add this one-line script into a text sensor :

top -b -n 1 | head -12 | tail -6 | sed '/top/d' | awk '{ printf "%-12.12s %-4s %-4s %-3s\n" , $12,$9,$10,$2}'

When I type this in a shell, this gives me the expected output

To add it into a textsensor, I did the following into my python file

test="\"top -b -n 1 | head -12 | tail -6 | sed '/top/d' | awk '{ printf (\"%10.10s %-8s %-8s %-5s\" , $12,$9,$10,$2)}' \" "
prog='sensor=program program='+test+' interval=4000 '

karamba.setTextSensor(widget, TopSensorText1, prog)


prog then equals to :
sensor=program program="top -b -n 1 | head -12 | tail -6 | sed '/top/d' | awk '{ printf ("%10.10s %-8s %-8s %-5s" , $12,$9,$10,$2)}' " interval=4000

But when I launch superkaramba, it complains about not finding the proper quote.

/bin/bash: -c: line 0: Caractère de fin de fichier (EOF) prématuré lors de la recherche du « ' » correspondant
/bin/bash: -c: line 1: Erreur de syntaxe : fin de fichier prématurée


Clearly the problem is the double-quote in the bash script. I cannot remove the double-quote because printf does not work anymore.

The script below works fine:
test="\"top -b -n 1 | head -12 | tail -6 | sed \'/top/d\' | awk \'{ print $12,$9,$10,$2 }' | column -t \""

Does any bash/superkaramba guru how to find to find a work-around ? I spent a week on that, and I am running dry.

Thanks

Fabien
 
Old 05-02-2010, 06:22 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Hi. I'm not an expert, but as you already pointed out the problem is due to any double quote appearing inside the test string (which is embedded in double quotes by itself), isn't it? Well.. I can only suggest an alternative which doesn't need mandatory double quotes:
Code:
printf '%10.10s %-8s %-8s %-5s\n' $(ps -eo comm,%cpu,%mem,user | sort -k2 -r | head -7)
This uses the bash built-in printf for which you can use single quotes as well. The arguments - passed through command substitution - are from the ps command, which let you select the output fields. You can compare it with the results of the top command and eventually refine the query and/or the format string. Hope this helps.
 
Old 05-02-2010, 11:45 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,804

Rep: Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069
Quote:
test="\"top -b -n 1 | head -12 | tail -6 | sed '/top/d' | awk '{ printf (\"%10.10s %-8s %-8s %-5s\" , $12,$9,$10,$2)}' \" "
Would seem you are looking for command substitution but are not using the right tools (apologise if I have misunderstood)

Should be:
Code:
test=$("top -b -n 1 | head -12 | tail -6 | sed '/top/d' | awk '{ printf (\"%10.10s %-8s %-8s %-5s\" , $12,$9,$10,$2)}' ")
You can of course use the backticks (``) but I think this is clearer.
 
Old 05-03-2010, 04:23 PM   #4
fabienhenon
LQ Newbie
 
Registered: Mar 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks

Hi !

Thanks for the quick reply and your answer

Actually, I found a way between the two replies and it works fine.

I wanted to use top (ps is not accurate enough)
here is the tweaked line :
printf '%-12.12s %6s %6s %-8.15s\n' $(top -b -n 1 | head -11 | tail -5 | sed '/top/d' | awk '{print $12,$9,$10,$2}')

Thanks to both of you for the help

Fabien

Quote:
Originally Posted by colucix View Post
Hi. I'm not an expert, but as you already pointed out the problem is due to any double quote appearing inside the test string (which is embedded in double quotes by itself), isn't it? Well.. I can only suggest an alternative which doesn't need mandatory double quotes:
Code:
printf '%10.10s %-8s %-8s %-5s\n' $(ps -eo comm,%cpu,%mem,user | sort -k2 -r | head -7)
This uses the bash built-in printf for which you can use single quotes as well. The arguments - passed through command substitution - are from the ps command, which let you select the output fields. You can compare it with the results of the top command and eventually refine the query and/or the format string. Hope this helps.
 
  


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
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 05:44 PM
Bash - double quotes don't protect Exclamation marks geoff_f Programming 15 04-15-2007 10:10 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM
In BASH shell, what is the difference in usage between single and double quotes? davidas Linux - Newbie 2 04-05-2004 04:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 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