Reputation: 133
Consider the following three commands:
$ echo -n "string1" | md5sum
34b577be20fbc15477aadb9a08101ff9 -
$ echo -n "string2" | md5sum
91c0c59c8f6fc9aa2dc99a89f2fd0ab5 -
$ echo -n "string3" | md5sum
9e6dc8685bf3c1b338f2011ace904887 -
Now, we would like to have a command functioning like that:
$ echo -n "string1 string2 string2" | xargs md5sum
34b577be20fbc15477aadb9a08101ff9 -
9e6dc8685bf3c1b338f2011ace904887 -
9e6dc8685bf3c1b338f2011ace904887 -
Howevery, the output of
$ echo -n "string1 string2 string2" | xargs md5sum
is:
md5sum: string1: No such file or directory
md5sum: string2: No such file or directory
md5sum: string3: No such file or directory
Can you help us fix this ?
We do not want to execute 3 processes for md5sum, we would really like to execute 1 process on 3 input strings (this is why xargs came into play... right?)
We do not want to write any file to disk, we want it to be as fast as RAM accessing.
Upvotes: 0
Views: 410
Reputation: 295650
md5sum
does not support any usage mode where a single invocation can read more than one distinct item to hash from stdin. Consequently, no possible combination of xargs
or other shell tools can invoke it in a manner that does what you want. (Creative use of process substitution might be possible on platforms where md5sum
allows named FIFOs to be passed to it as arguments, but on at least one platform where I tested, the command works only on regular files).
Consider using a different tool:
hashmany() {
python -c '
import hashlib, sys
for arg in sys.argv[1:]: print hashlib.md5(arg).hexdigest()
' "$@"
}
...thereafter, your code can run:
hashmany string1 string2 string3
...with the output:
34b577be20fbc15477aadb9a08101ff9
91c0c59c8f6fc9aa2dc99a89f2fd0ab5
9e6dc8685bf3c1b338f2011ace904887
Upvotes: 3