Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove (STDIN)= label in BASH

Tags:

bash

stdin

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

like image 624
MiRAGE Avatar asked Oct 30 '25 19:10

MiRAGE


2 Answers

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.

like image 137
Mark Setchell Avatar answered Nov 01 '25 17:11

Mark Setchell


Probably not the best solution, but you can use awk to select 2nd column. Like this:

echo -n $temp_var1 | openssl sha1 | awk '{print $2}'
like image 40
arenaq Avatar answered Nov 01 '25 18:11

arenaq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!