When I try to encode the password variable into md5sum, it is giving me a string with the md5sum encoding and a dash at the end. And when I try to put a dash after it, it doesn't work. So my questions is: How can I remove the dash on the md5sum output of the linux terminal? Here is my code:
#!/bin/bash
read -p 'Password: ' passwordInput
Password=$(echo -n "$passwordInput" | md5sum)
if [ $Password = "0cbc6611f5540bd0809a388dc95a615b" ]; then
clear
echo "Password is good"
else
clear
echo "Password is bad"
fi
exit 0;
#!/bin/bash
read Test
Password=$(echo -n "$Test" | md5sum | cut -d' ' -f1)
echo ""
if [ $Password = "d41d8cd98f00b204e9800998ecf8427e" ]; then
clear
echo "WhOop WHOop"
else
echo ":("
fi
You have three problems.
First, your question is not very clear. Please update it to state clearly just what you're asking (how you want your program to behave, and how it fails to do so). In this case it's not too hard to figure out from your code, but that's not always true. You write
I don't get the "-" out of the linux md5sum.
but in fact the problem is that you are getting the "-" (and probably more as well).
(UPDATE: Better now, thanks.)
Second, the output of the md5sum
includes some extra information after the displayed checksum, and you need to remove it. (It would be nice if md5sum
had an option to print just the checksum, but it doesn't.) Fortunately, it's easy to strip the extra information; it's just a space character and everything following it.
Third, because you're using single quotes:
Password="$(echo -n '$Test' | md5sum)"
the string you're passing to md5sum
is literally '$Test'
, not the value of your variable.
Fixing the second and third problems:
#!/bin/bash
read Test
Password="$(echo -n "$Test" | md5sum | sed 's/ .*$//')"
echo ""
if [ "$Password" = "d41d8cd98f00b204e9800998ecf8427e" ]; then
clear
echo "WhOop WHOop"
else
echo ":("
fi
I'd normally criticize your use of the clear
command, but in this case you're erasing a password that the user entered. But you're not clearing the screen if the user enters an incorrect password. Bash's read
command has an option to suppress echoing of input, and this is the perfect use for it. Here's how I might do it:
#!/bin/bash
read -p "Password: " -s Test
Password="$(echo -n "$Test" | md5sum | sed 's/ .*$//')"
echo ""
if [ "$Password" = "d41d8cd98f00b204e9800998ecf8427e" ]; then
echo "WhOop WHOop"
else
echo ":("
fi
(Incidentally, the empty string is a lousy password, as is anything whose md5sum can be found with a quick Google search -- like "Test
".)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With