Quote:
Originally Posted by jaybutts
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