Search code examples
bashstdin

remove (STDIN)= label in BASH


i am having a problem with (STDIN)

i have written the following bash script to calculate the sha1 hash of a string and output it in upper-case formatted ready for copying into a plist file for example.

here is the script :-

#!/bin/bash        

echo 'Enter String :-';    

read temp_var1;                                          #Read string

hash=$(echo -n $temp_var1 | openssl sha1);               #Copy hash into variable $hash

uphash=$(echo $hash | tr '[:lower:]' '[:upper:]';);      #Convert hash to Upper-case

echo 'Below is the UPPERCASE SHA1 Hash of your string';

echo '  </yourHash>'$uphash'</yourHash>';                #Echo formatted string

This whole script works well apart for one key problem. the output is not what i want it to be. for example with an input of

Hello, World!

i want to get the following output:-

Below is the UPPERCASE SHA1 Hash of your string
</yourHash>0A0A9F2A6772942557AB5355D76AF442F8F65E01</yourHash>

instead what i am getting is this :-

Below is the UPPERCASE SHA1 Hash of your string
</yourHash>(STDIN)= 0A0A9F2A6772942557AB5355D76AF442F8F65E01</yourHash>

my question is. How do i remove this (STDIN)= lable and the space that follows it?

many thanks in advance for any help you can offer.

MiRAGE


Solution

  • You can strip it off the front using parameter substitution like this:

    a="(STDIN)= 0A0A9F2A6772942557AB5355D76AF442F8F65E01"
    b=${a#*= }    # Strip up to "=" sign
    echo $b
    0A0A9F2A6772942557AB5355D76AF442F8F65E01
    

    I suspect that there is an option to openssl to stop it generating the "(STDIN)" part, but mine doesn't do that.

    If you want to learn more about this sort of thing, have a look here.