You're doing var
assignment in wrong way.
var=$passvar|md5sum
You're supposed to send "$passvar"
to md5sum
. It won't go through pipe |
by itself, so you either need to use echo
, or printf
. That will output things to stdout
stream. You can capture that into variable with command substitution $(...)
. In other words, you should be doing:
var=$( echo "$passvar" | md5sum )
Also, please remember to quote the variables to preserve the integrity of data stored in them and prevent word splitting.
Other issues:
$ read -sp 'Your password:' passvar
Your password:$ var=$( echo "$passvar" | md5sum )
$ echo "$var"
2b00042f7481c7b056c4b410d28f33cf -
Notice two things here: there's no newline after "Your password", so you might want to add echo
after that command. Second, notice that md5sum
outputs filename after the calculated md5 checksum, which in this case is -
or stdin
stream. You might want to consider getting rid of that part via any text processing utility that will let you cut away words, like awk
:
var=$( echo "$passvar" | md5sum | awk '{print $1}')
Alternatively, you can just make use of bash
's read and here-string <<<
(see this for more info on that operator) :
$ read var trash <<< $( echo "$passvar" | md5sum )
$ echo "$var"
2b00042f7481c7b056c4b410d28f33cf
Last but not least, your #!
line is missing /
before bin
- it should be #!/bin/bash
.
The complete script should be as so:
#!/bin/bash
read -p 'Your login: ' uservar
read -sp 'Your password: ' passvar
printf "\n"
read var trash <<< "$(printf "%s" "$passvar" | md5sum)"
printf "%s:%s" "$uservar" "$var" #> ~/results.txt
Note that I commented out redirection to file, just to see the output on the terminal for testing purpose. Here's how it works:
$ ./testing.sh
Your login: johndoe
Your password:
johndoe:912ec803b2ce49e4a541068d495ab570$
For future reference, you can add set -x
after #!/bin/bash
(given that you've properly defined the #!
line, of course) to troubleshot the script and see what commands actually run and were errors might arise. Or just use shellcheck online tool.
md5sum
isn't really meant for encryption.md5sum
and similar utilities are hashes, i.e. they're meant to just go one way - get a hash sum out of them and used for verifying if data is correct or not, and not really meant to be "decrypted" (although, there's ways to get original string with weak hashes likemd5sum
andsha1sum
, which was cracked last year). Just FYI – Sergiy Kolodyazhnyy Nov 30 '17 at 08:11