Menu

Can I specify "-v" for &quo...

Help
2011-07-31
2013-03-13
  • Bogdan Iosif

    Bogdan Iosif - 2011-07-31

    Hi,

    Is there any way to specify the value for the "-v" action of the "ed" command via stdin?

    I imagined it should've worked like this (Windows):

    type "FileWithValue.txt"|xml ed -u /root/ElementToBeUpdated -v - "FileToBeEdited.xml"
    

    With my version of XMLStarlet the result of running the above would set the value of "ElementToBeUpdated" to "-" and I would want for that value to be set to the content of the file "FileWithValue.txt".

    If it would be possible to specify that value via stdin, I think 2 problems could be solved:
    1. Much safer to set complex/computed/injected values that otherwise could break/change what is intended to be executed by the shell
    2. Would allow specifying multi-line values. AFAIK, currently there is no way to specify multi-line values.

    Regards,
    Bogdan

    P.S: Result of running "xml.exe -version" for me:

    1.2.1
    compiled against libxml2 2.7.8, linked with 20708
    compiled against libxslt 1.1.26, linked with 10126

     
  • Noam Postavsky

    Noam Postavsky - 2011-08-01

    AFAIK, currently there is no way to specify multi-line values.

    That's only true in a crippled shell like cmd.exe:

     bash$ xml ed -u /root/ElementToBeUpdated -v one$'\n'two FileToBeEdited.xml
    
     PowerShell> xml ed -u /root/ElementToBeUpdated -v one`ntwo FileToBeEdited.xml
    

    And if you want to get the value from a file:

     bash$ xml ed -u /root/ElementToBeUpdated -v "$(cat FileWithValue.txt)" FileToBeEdited.xml
    

    I'm sure that's possible in PowerShell too.

    I have been thinking about adding an XPath function to execute a
    process, that way you could pass different arguments based on the node
    value/attributes:

     xml ed -u /elem -x "xstar:exec('SomeCommand', '--option', @AttributeValueHasArgument)" file.xml
    

    A readfile function could be useful that way too:

     xml ed -u /elem -x "xstar:readfile(@AttributeValueHasFilename)" file.xml
    

    Another idea was to use a process as a filter, for example to upcase
    the value of all elements called "elem":

     bash$ xml ed --filt:upcase awk '{print toupper($0)}' \; -u //elem -x 'filt:upcase(.)' file.xml
    
     
  • Bogdan Iosif

    Bogdan Iosif - 2011-08-02

    Thanks for the reply, I understand the solution. Unfortunately, it doesn't work with cmd and I don't really know why as I found an ugly hack to embed a #13#10 on the command line ( see the last comment, by Jiriki, on this page: http://www.computing.net/answers/programming/insert-carriage-return-w-batch/13548.html )

    I mean no offense and I appreciate your reply but I think it's more a hack than a definitive solution to the problem.

    The optimal way to specify a mult-iline value would be via a file that I should be able to reference somehow by name on XmlStarlet's command line. Expanding the whole file's content on the command line is not practical for large files and it can easily lead to the shell executing things you didn't mean.

    /Bogdan

     
  • Bogdan Iosif

    Bogdan Iosif - 2011-08-02

    An example from CURL's man: The -d switch allows you to specify some data for an HTTP request issued with CURL. The data can be specified inline or via a file.

    -d/-data <data>
           (HTTP)  Sends  the  specified data in a POST request to the HTTP
           server, in the same way that a browser  does  when  a  user  has
           filled  in an HTML form and presses the submit button. This will
           cause curl to pass the data to the server using the content-type
           application/x-www-form-urlencoded.  Compare to -F/-form.

           -d/-data  is  the  same  as  -data-ascii.  To post data purely
           binary, you should instead use the -data-binary option. To URL-
           encode the value of a form field you may use -data-urlencode.

           If  any of these options is used more than once on the same com-
           mand line, the data pieces specified  will  be  merged  together
           with  a  separating  &-symbol.  Thus,  using  '-d name=daniel -d
           skill=lousy'  would  generate  a  post  chunk  that  looks  like
           'name=daniel&skill=lousy'.

           If  you  start  the data with the letter @, the rest should be a
           file name to read the data from, or - if you want curl  to  read
           the  data  from stdin.  The contents of the file must already be
           URL-encoded. Multiple files can also be specified. Posting  data
           from  a file named 'foobar' would thus be done with -data @foo-
           bar.

     
  • Noam Postavsky

    Noam Postavsky - 2011-08-08

    The optimal way to specify a mult-iline value would be via a file that I should be able to reference somehow by name on XmlStarlet's command line. Expanding the whole file's content on the command line is not practical for large files and it can easily lead to the shell executing things you didn't mean.

    Yes, I think you are right about this. Currently I'm leaning towards having readfile as an XPath function, that seems like the most flexible way of doing it.

     

Log in to post a comment.