0

On my linux shell (ubuntu) if I type the following command:

echo -n -e "THIS IS A TEST" | md5sum

I get the md5 hash value of: 1586CFFAFA39E38959477DA9EAA41C31

If I do the following:

awk -f short.awk

where short.awk contains:

BEGIN {

    print "HELLO"

    md5sum_command = sprintf("echo -n -e \"%s\" | md5sum", "THIS IS A TEST");

    if ( (md5sum_command | getline line) > 0) {
        result = line;
        type = "linux";
        hash_command = "echo -n -e \"%s\" | md5sum";
        printf("Command: %s\nResult = %s\n", md5sum_command, result);
        printf("It looks like you are on a linux environment, so the md5sum command will be used\n");
    } else {
        result = "FAILED";
    }
    close(md5sum_command);

}

I get an md5 hash value of: a842e5b39bf5aef1af5d4a0ef7acf8e9

I cannot figure out what the issue is.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • If I remove the -e from the awk script is seems to work for this string, but I need the -e for strings with newlines in them. – Nicholas Antimisiaris Oct 06 '14 at 10:18
  • Why are you using `getline` within the `BEGIN` block, rather than using awk's built-in line reading behaviour? – Tom Fenech Oct 06 '14 at 11:19
  • possible duplicate of [Why is an MD5 hash created by Python different from one created using echo and md5sum in the shell?](http://stackoverflow.com/questions/5693360/why-is-an-md5-hash-created-by-python-different-from-one-created-using-echo-and-m) – l0b0 Oct 06 '14 at 11:28
  • Tom, the concept is to first test which environment the script is running on (macos has md5 function and linux has md5sum function). But this issues is not relevant to the question - the hash result from awk does not match the result from direct shell execution. – Nicholas Antimisiaris Oct 06 '14 at 11:37
  • possible duplicate of ["echo -n" works fine when executing script with bash, but not with sh](http://stackoverflow.com/questions/17865587/echo-n-works-fine-when-executing-script-with-bash-but-not-with-sh) – Joe Oct 06 '14 at 12:29
  • But now I have a follow-up issue. Why does this linux shell command not give the right md5 hash? /bin/echo -ne "This is a\n simple test" | md5sum ??? – Nicholas Antimisiaris Oct 06 '14 at 13:04
  • But is does work without the -e !!! This is really weird. – Nicholas Antimisiaris Oct 06 '14 at 13:17

1 Answers1

1

You're running two different versions of echo. You get the builtin from /bin/bash in your interactive shell and the builtin from /bin/sh when running via awk. They behave differently.

Use /bin/echo to get consistent behaviour in both cases.

Joe
  • 29,416
  • 12
  • 68
  • 88
  • um ... echo is a independent binary. It is usually shipped together with coreutils, though - i find it REALLY hard to believe that any linux-binary out there uses a "builtin" echo, that'd be VERY weird if not error-prone – specializt Oct 06 '14 at 12:35
  • 1
    See [this question](http://unix.stackexchange.com/questions/1355/why-is-echo-a-shell-built-in-command) for an explanation of why `echo` is a builtin. – Joe Oct 06 '14 at 12:37
  • Joe - that was the issue! THANK YOU for your insight. – Nicholas Antimisiaris Oct 06 '14 at 12:45