Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

362
Visualizações
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 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;
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

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
over 4 years ago · Santiago Trujillo Relatório

0

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".)

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda