LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-31-2012, 01:34 PM   #1
Bill Gates 666
Member
 
Registered: Dec 2003
Location: Cambridge
Distribution: Arch, Gentoo, OpenSUSE
Posts: 70

Rep: Reputation: Disabled
Question BASH Scripting : Quoting Variables Catch 22!!


Hi

I'm trying to fix a bug (the erroneous striping of \ escape characters in unit names) in the ARCH-Linux SystemD bash-completion script.

The crucial line is:
Code:
COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
Basically I want to write something like:
Code:
COMPREPLY=( $(compgen -W '$comps' -- '$cur') )
so I get "protection" for the \ escape character (which starts to get swolled up otherwise!!) but also get expansion for variables $comps and $cur (which the ' quote prevents).

I've tried (a gross hack) creating the string first, then executing it, but =Fail - something like:
Code:
local command="compgen -W '$comps' -- '$cur'"
COMPREPLY=( $(`$command`) )
$COMPREPLY is just an empty string each time... $command contains the correct characters...

Any BASH experts out there able to help out a noob??

Mr Gates
 
Old 04-01-2012, 08:37 AM   #2
jaybutts
LQ Newbie
 
Registered: Apr 2012
Location: Detroit
Distribution: Centos, Gentoo, Debian, Ubuntu, FreeBSD
Posts: 5

Rep: Reputation: Disabled
Im not really sure the extra set of parenthesis is need but here some things you can try aside from that:
try:
Code:
COMPREPLY=( $(compgen -W "${comps}" -- "${cur}") )
Again not sure why you have the extra paranthesis, simply try this:

Code:
COMPREPLY=$(compgen -W "${comps}" -- "${cur}")
Aside form that I also don't know what the -- does? or what your code really does, it might help to try and explain it a little more.
 
Old 04-01-2012, 09:46 AM   #3
Bill Gates 666
Member
 
Registered: Dec 2003
Location: Cambridge
Distribution: Arch, Gentoo, OpenSUSE
Posts: 70

Original Poster
Rep: Reputation: Disabled
Question

Quote:
Originally Posted by jaybutts View Post
Aside form that I also don't know what the -- does? or what your code really does, it might help to try and explain it a little more.
@jaybutts

Sorry my bad for not giving enough details!!

The extra bracketing is convert a returned list of SystemD subcommands or Unit names to an array of potential responses! Just to re-iterate it's not my code at all. Like I mentioned it's the ARCH-Linux SystemD bash-completion script. All the script does is to provide command line completion for SystemD commands entered in a BASH shell (i.e. when you pressed the TAB key it tries to match against all valid responses).

With your suggestion (remove extra bracketing) the command-line completion variable $COMPREPLY will contain all the potential responses in a big list (individual entries separated by spaces). So that is not going to work!
Code:
$COMPREPLAY='enable isolate delete start daemon-reexec daemon-reload default dot dump emergency exit halt kexec list-jobs list-units list-unit-files poweroff reboot rescue show-environment disable reenable snapshot load link restart reload-or-restart set-environment unset-environment cancel unmask stop condstop kill try-restart condrestart is-active is-enabled status show mask preset reload condreload reload-or-try-restart force-reload reset-failed'

The desired behaviour is to return an array containing individual valid responses, e.g.:
Code:
$COMPREPLAY[0]='enable'
$COMPREPLAY[1]='isolate'
...

The problem I am trying to tackle is that SystemD Unit names have to use a name mangling system. If they contain (normally command-line escaped) things such as brackets, etc then these must be converted to escaped HEX code equivalents. The '-' character is used to represent Linux directory back-slashes ('/') - so must also be mangled.

E.g.:
Code:
mnt-robs\x2dserver-external-Music.mount
would specify a mount-point Unit (for any specified filesystem) in the following (real Linux FS hierarchy) mount-point:
Code:
/mnt/robs-server/external/Music
(where '\x2d'=HEX character code for '-')

See here for more info on the name mangling used.


I take the input Unit names and use awk mangle it to:
Code:
/mnt/robs\\x2dserver/external/Music
(doubling the number of '\' forward slashes so that the name works on the BASH commandline - since '\' characters must be escaped).

The problem is that the double quote " symbol around variables ${comps} and ${cur} results in them being transformed by the shell with '\\' combinations being replaced by '\'. So I would get back to:
Code:
/mnt/robs\x2dserver/external/Music
When this is passed as a Unit name to the SystemD systemctl command

E.g.
Code:
sudo systemctl enable /mnt/robs\x2dserver/external/Music
would fail as an invalid Unit name as the BASH shell interprets the '\x' as an escaped 'x' character.

What I want the commandline completion to do is give:
Code:
sudo systemctl enable /mnt/robs\\x2dserver/external/Music
Any thoughts? Sadly I am a total BASH scripting noob...
Bob
 
Old 04-05-2012, 11:22 AM   #4
alinas
Member
 
Registered: Apr 2002
Location: UK, Sywell, EGBK
Distribution: RHEL, SuSE, CentOS, Debian, Ubuntu
Posts: 60

Rep: Reputation: 20
Not sure I understood the details, but it feels like a potential job for eval.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting - find command with variable substitution and quoting bgeddy Programming 4 07-29-2022 08:28 AM
bash scripting and integer variables. stf92 Linux - Software 1 06-06-2010 06:30 PM
shell scripting/bash/variables mayaabboud Linux - Newbie 13 01-06-2008 12:16 PM
shell scripting/bash/variables mayaabboud Linux - General 4 01-06-2008 08:17 AM
server variables in bash scripting basher400 Linux - Newbie 4 04-11-2005 06:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:26 AM.

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