You are not logged in.

#1 2012-04-01 13:29:37

bobwya
Member
Registered: 2012-01-09
Posts: 36

[SOLVED] 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.

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 roll) 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?? sad

Thanks
Bob

Last edited by bobwya (2012-04-02 10:00:44)

Offline

#2 2012-04-02 01:36:22

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [SOLVED] BASH Scripting : Quoting Variables Catch 22!!

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

#3 2012-04-02 02:01:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [SOLVED] BASH Scripting : Quoting Variables Catch 22!!

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

#4 2012-04-02 09:10:07

bobwya
Member
Registered: 2012-01-09
Posts: 36

Re: [SOLVED] BASH Scripting : Quoting Variables Catch 22!!

Trilby wrote:

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

#5 2012-04-02 09:19:36

portix
Member
Registered: 2009-01-13
Posts: 757

Re: [SOLVED] BASH Scripting : Quoting Variables Catch 22!!

You don't need awk for this, you can use the builtin parameter expansion instead:

$ TEMP="one\\two"
$ echo ${TEMP/\\/\\\\}
one\\two

Offline

#6 2012-04-02 10:00:08

bobwya
Member
Registered: 2012-01-09
Posts: 36

Re: [SOLVED] BASH Scripting : Quoting Variables Catch 22!!

portix wrote:

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?!! cool

But I was using
Thanks
Bob

Offline

Board footer

Powered by FluxBB