简体   繁体   中英

Removing '-' on linux md5sum

When I try to encode the password variable into md5sum, it is giving me a string with the md5sum encoding and<\/strong> 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.

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

(It would be nice if md5sum<\/code> had an option to print just<\/em> 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.

 But you're not clearing the screen if the user enters an incorrect password. Bash's read<\/code> command has an option to suppress echoing of input, and this is the perfect use for it.  Here's how I might do it:

"

The "-" in the md5sum indicates, that you are using standard input for md5sum. If you would be using md5sum for a file, a filename would be displayed in that place.

To follow on from @user1918305's suggestion, md5 will operate on a string input.

For example

#!/bin/bash

$ printf "hello world" | md5sum
5eb63bbbe01eeed093cb22bb8f5acdc3  -
$ printf "hello world" | md5sum | grep -o '^\S\+'
5eb63bbbe01eeed093cb22bb8f5acdc3
$ printf "hello world" | md5
5eb63bbbe01eeed093cb22bb8f5acdc3

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