Wednesday 15 February 2012

How to escape a variable in bash when passing to command line argument -


I have a bass script (signin) that uses some window paths with spaces in them. Consequently, I have escaped from the space with \ one in my variable definition. Everything inside the script works fine though, I need to pass this variable as an argument for the command line executable. When I do this, I get confused with my escape.

Sample Non-Functional Script:

  #! / Bin / sh # File Path Destination = "/ Sigrid / C / Documents and Settings / Eric / My Documents / Attachments =" \ n2013-12-12.pdf "body =" test body "Recipient =" asdf@asdf.com "# Create attachment $ $ for attachment file =" "; file = $ {file // [\\ n] /} touch $ file mv $ file" $ destination path / $ file "args =" $ ARGS-A $ Destination / $ file "done # Send Email Email-e $ body | Email --from-addr nosender@mydomain.com --from-name" Automatic CSS Downloader "--subject" New file downloaded Output from the script: $ args from eric@mydomain.com   

"CSS"

  $ bash -x test.sh + destination path = '/ Sigrid / C / Documents and Settings / Eric / My Documents / 'Attachment =' \ n2013 -12-12 PDF '+ Body =' Test Body '+ Recipient- asdf@asdf.com + Argus = +' $ Attachment ' + File = 2013-12-12 PDF + touch 2013-12-12 .pdf + mv 2013-12-12.pdf '/ cygdrive / c / document and settings / eric / my document // 2013-12-12 Listing Features of '.pdf' mv: `2013-12-12.pdf ': Aman Logic + args = '-a / signage / c / document and settings / eric / my document // 2013-12-12.pdf' + echo-a test body + email - from-adress noserand @ ma'domain - 'automatic css Downloader '--subject' CSS '- A / Sigradev / C / Documents and Settings / Eric / New Files (S) Downloaded from My Documents // 2013-12-12.pdf Eric @ Maedoman com Email: Alert: Email Address 'and' invalid is being omitted ... Email: FATAL: Attachment not opened Can not find: / cygdrive / c / document: No such file or directory   

It is not as you can see the space saved in the path being exported into the $ ARGES variable I'm assuming the error comes on "Argus = ..."

I have tried to use double quotation marks in the destination post but there is no benefit, but I am not sure how to save the destination path.

If I try to repeat the $ Destination bid in the Argos = line, then the output is also spoiled with a bunch of extra quotes.

How can I work it out? I have tried to play with the $ IFS variable, but I really do not know what I'm doing with it, and it can not seem to work with it, though I doubt there is something with the solution $ IFS

There is no good way to do this without the array ( eval There is no good way to do this using, but the array is simple.)

The problem is that you want to have email < / Code>, but you want to store all of those arguments in a bash variable, there is no way to do this: you can not use the bash variable as an argument ( "$ arg" ) Because of putting it in the form or you can divide it into many words like it is divided into ( $ arg ), but you can not divide it when you split When the variables were created, they were escaped because bash only remembers the strings assigned to a variable, not the escape marks.

However, you can do this with an array, because you can make an array element of each filename exactly, and you can insert an array as an argument in the form of each element .

How is it:

  # file path = "/ cygdrive / c / document and settings / Eric / my document /" attachment = "\ n2013-12- 12.pdf "body =" test body "recipient =" asdf@asdf.com "# attachment RRG = () $ for attachment file; File = $ {file // [\\ n] /} touch $ file mv $ file "$ destinationPath / $ file" args + = (- "$ destination path / $ file") Send email # echo -e $ body | Email --from-addr nosender@mydomain.com \ - -NAM "Automatic CSS Downloader" \ --subject "New file from CSS" is downloaded "\" $ {Args [@]} "Eric @ Mademon Com   

You may also want to create the attachment variable in a code; With the presence of the new character, I am assuming that you are actually setting it in something more complex, so I did not change the code. But your current code will not work if the attachment name has a place in it (and I'm pretty sure the parameter in the new code for will be removed in detail, if you do not have < The value of Code> $ IFS has been changed, so file = $ {file // [\\ n] /} should not be required.)

No comments:

Post a Comment