1

I have a little script file called testing.sh where I am asking user to enter his login and password. Then, the password is getting encrypted with md5sum command and the result is stored in a file results.txt in a form login:md5password. However, when I am viewing the results.txt file, I only get login:, the encrypted password isn't concatenated. What am I doing wrong?

Here is the testing.sh file:

#!bin/bash
read -p 'Your login: ' uservar
read -sp 'Your password: ' passvar

var=$passvar|md5sum
echo $uservar:$var > ~/Desktop/results.txt

results.txt file:

login:
Evgeny
  • 13
  • 3

2 Answers2

3

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.

pa4080
  • 30,621
1

The following script also working for me.

#!/bin/bash
echo -n 'Your login:'
read uservar

echo -n 'Your password:'
read -s passvar

printf "\n"

encrpt=$(echo "$passvar" | md5sum | awk '{print $1;}')

echo "$uservar":"$encrpt" > /home/bond/result.txt

Output

bond@hp-pc:~$ ./test.sh 
Your login:bond
Your password:
bond@hp-pc:~$ cat result.txt 
bond:22c291f0335f869c154d8081854d9119
Rooney
  • 975