Xorwell
Xorwell

Reputation: 133

one single md5sum process on multiple input strings (via xargs ?)

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 ?

Upvotes: 0

Views: 410

Answers (1)

Charles Duffy
Charles Duffy

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

Related Questions