venerdì, settembre 19, 2014

bash: how to put a file in a variable mantaining it's integrity

From a file:

$ md5sum file
a69f4d07c630d1285a31354c7cb0f31d  file

$ myvar=$(< file)

$ echo "${myvar}" | md5sum
a69f4d07c630d1285a31354c7cb0f31d  -

From stdin inside a script:

$ cat myscript
#!/bin/bash

myvar=$(< <(cat))

echo "${myvar}"

$ md5sum file
a69f4d07c630d1285a31354c7cb0f31d  file

$ cat file | ./myscript | md5sum
a69f4d07c630d1285a31354c7cb0f31d  -



Thanks to Daniele Pilenga for the nested parentheses  in the second case !

EDIT: it seems that it doesn't work for binary files, probably because it seems that bash can't store NUL value inside a variable(http://superuser.com/questions/399685/how-to-keep-line-feed-in-result-returned-by-bash-command)... using "base64" command help in thins case :-)

Nessun commento: