You are not logged in.
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.
Cross-posted to Linux Questions.
The crucial line is:
COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
Basically I want to write something like:
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:
local COMMAND="compgen -W '$comps' -- '$cur'"
COMPREPLY=( $(`$COMMAND`) )
$COMPREPLY is just an empty string each time... $COMMAND (echo'd) definitely contains the correct string (i.e. copying it to the command line and executing it does work)...
Any BASH experts out there able to help out a noob??
Thanks
Bob
Last edited by bobwya (2012-04-02 10:00:44)
Offline
I don't think you understand the root of your problem.
If you do this
COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
with the variable 'comps' containing a string with one or more embedded \'s, then compgen would get passed that exact string (with the embedded \'s) as it's second parameter. Bash would not swollow the \'s.
Since I don't know what compgen does or how the variables comps and cur are set in this context, I can't tell you anything more about what your problem is or how you could solve it.
In your last example, the backtics are possibly causing your problem. Try
...
COMPREPLY=( $($COMMAND) )
instead of
...
COMPREPLY=( $(`$COMMAND`) )
(This probably still won't do what you want, however.)
Offline
Can you give examples of the inputs and the resulting output along with the desired output?
If I'm understanding correctly, the escaped characters would need to be "protected" at the previous step - they need to be stored to the variable. For example, see the difference here:
$ export TEMP=one\ two
$ echo $TEMP
one two
$ export TEMP="one\ two"
$ echo $TEMP
one\ two
If you save a variable without the quotes, as in the first example, then the escapes are already "processed" and gone; they can't be "protected" if they are already gone.
Last edited by Trilby (2012-04-02 02:03:36)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Can you give examples of the inputs and the resulting output along with the desired output?
....
My problem is:
$ export TEMP=one\\two
$ echo $TEMP
one\two
$ export TEMP="one\\two"
$ echo $TEMP
one\two
But I want something like (preserving the forward slashes '\' in the output):
$ export TEMP="one\\two"
$ echo '$TEMP'
one\\two
But that is impossible since the variable expansion won't take place when using single quotes '...!
The closest I've got would be variations of the code (below) to "protect" the forward slashes:
$ export TEMP="one\\two"
$ echo "$TEMP" | awk '{gsub(/\\/,"\\\\\\\\"); print}'
The use of an awk 'hack' seems a bit heavyweight for the severity of the problem though!!
Thanks
Bob
Last edited by bobwya (2012-04-02 09:10:40)
Offline
You don't need awk for this, you can use the builtin parameter expansion instead:
$ TEMP="one\\two"
$ echo ${TEMP/\\/\\\\}
one\\two
Offline
You don't need awk for this, you can use the builtin parameter expansion instead:
$ TEMP="one\\two" $ echo ${TEMP/\\/\\\\} one\\two
You are a scholar and a gentleman sir!! Certainly put me on the right track!!
I found a page about this topic on the interwebs, since I (as it turned out) need:
$ TEMP="one\\two\\\\three\\four"
$ echo ${TEMP//\\/\\\\}
one\\two\\\\three\\four
as a replacement for the gsub as was using with awk (double back slash for global replacement if you didn't spot it!!)
Wow is BASH a crazy dude language or what?!!
But I was using
Thanks
Bob
Offline