Passing binary data as arguments in bash

前端 未结 5 697
余生分开走
余生分开走 2020-12-14 16:32

I need to pass binary data to a bash program that accepts command line arguments. Is there a way to do this?

It\'s a program that accepts one argument:



        
相关标签:
5条回答
  • 2020-12-14 17:05

    Save your binary data to a file, then do:

    script "`cat file`"
    
    0 讨论(0)
  • 2020-12-14 17:10

    Use the $'' quote style:

    script $'\x02\xc5\xd8'
    

    Test:

    printf $'\x02\xc5\xd8' | hexdump -C
    00000000  02 c5 d8
    
    0 讨论(0)
  • 2020-12-14 17:13

    Bash is not good at dealing with binary data. I would recommend using base64 to encode it, and then decode it inside of the script.

    Edited to provide an example:

    script "$(printf '\x02\xc5\xd8' | base64 -)"
    

    Inside of the script:

    var=$(base64 -d -i <<<"$1")
    
    0 讨论(0)
  • 2020-12-14 17:19

    How about this?

    $ script "`printf "\x02\xc5\xd8"`"
    
    0 讨论(0)
  • 2020-12-14 17:27
    script "`printf "\x02\xc5\xd8"`"
    script "`echo -e "\x02\xc5\xd8"`"
    

    test:

    # echo -n "`echo -e "\x02\xc5\xd8"`" | hexdump -C
    00000000  02 c5 d8                                          |...|
    
    0 讨论(0)
提交回复
热议问题