简体   繁体   中英

Getting md5sum hash from awk script

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM