LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 06-09-2016, 06:30 AM   #1
elocholero
LQ Newbie
 
Registered: Jul 2012
Posts: 9

Rep: Reputation: Disabled
Angry quote inside a variable


Code:
[root@localhost libexec]#  sudo -u jboss ./check_jmx -U service:jmx:remoting-jmx://localhost:4447 -O java.lang:type=MemoryPool,name="Perm Gen" -A Usage -K used | cut -d = -f 2
Anyway I could declare this as a variable, or redirect the output of this command to a one? This is already containing an quote mark, and it has to. Any help will be greatly appriciated!
 
Old 06-09-2016, 06:39 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by elocholero View Post
Anyway I could declare this as a variable, or redirect the output of this command to a one?
Yes
Quote:
Originally Posted by elocholero View Post
This is already containing an quote mark, and it has to. Any help will be greatly appriciated!
Just escape the quote marks (") as appropriate.
 
Old 06-09-2016, 07:34 AM   #3
elocholero
LQ Newbie
 
Registered: Jul 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
For some reason, it does not behave the same, when I am using escape:

Quote:
[root@localhost libexec]# perm_check="sudo -u root ./check_jmx -U service:jmx:remoting-jmx://localhost:4447 -O java.lang:type=MemoryPool,name=\"Perm Gen\" -A Usage -K used | cut -d = -f 2"; $perm_check
JMX CRITICAL Unterminated quoted value

[root@localhost libexec]# perm_check="sudo -u root ./check_jmx -U service:jmx:remoting-jmx://localhost:4447 -O java.lang:type=MemoryPool,name=\'Perm Gen\' -A Usage -K used | cut -d = -f 2"; $perm_check
JMX CRITICAL java.lang:type=MemoryPool,name=\'Perm

[root@localhost libexec]# perm_check="sudo -u root ./check_jmx -U service:jmx:remoting-jmx://localhost:4447 -O java.lang:type=MemoryPool,name='Perm Gen' -A Usage -K used | cut -d = -f 2"; $perm_check
JMX CRITICAL java.lang:type=MemoryPool,name='Perm
As w/o:

Quote:
[root@localhost libexec]# sudo -u root ./check_jmx -U service:jmx:remoting-jmx://localhost:4447 -O java.lang:type=MemoryPool,name="Perm Gen" -A Usage -K used | cut -d = -f 2
43166280

Last edited by elocholero; 06-09-2016 at 07:44 AM.
 
Old 06-09-2016, 07:59 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quoting gets really difficult when command lines are parsed multiple times, in this case, (1) when setting the variable, (2) when invoking that variable as a command, and (3) when sudo passes the command line to a shell for actual execution.

In general, variables are the wrong place to store command lines. It's usually much easier to define a shell function instead:
Code:
function perm_check () {
sudo -u jboss ./check_jmx -U service:jmx:remoting-jmx://localhost:4447 -O java.lang:type=MemoryPool,name="Perm Gen" -A Usage -K used | cut -d = -f 2
}

perm_check
 
2 members found this post helpful.
Old 06-09-2016, 08:17 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
probably | should be quoted too, that makes it really difficult (look for evaluation order)
 
Old 06-09-2016, 10:45 AM   #6
elocholero
LQ Newbie
 
Registered: Jul 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thank you guys, straight to the point. What do I pay you rknichols?
 
Old 06-09-2016, 12:20 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
I really can't claim that idea as my own. Maybe Al Gore came up with it back when he was inventing the Internet.

Last edited by rknichols; 06-09-2016 at 12:21 PM.
 
  


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
[SOLVED] Python (Jinja2) - Use variable inside a variable vikas027 Programming 1 10-07-2015 12:13 AM
[SOLVED] How to pass a single quote inside of a single quote... trist007 Linux - Newbie 2 02-09-2011 07:07 PM
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 11:31 AM
when to quote or not to quote variable names - mktemp problem glinux Linux - Newbie 3 02-20-2009 04:37 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM

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

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