• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1350
  • Last Modified:

catch md5sum output

I am writing a script that when run will ask the user to enter a password. The function will be something like:

function func_somethin() {
read -p "Enter User Password: " DBPASS
echo '${DBPASS}' | md5sum | grab MD5 here and place into variable of ${MD5PASS}
}

${MD5PASS} will be placed into a SQL query.

The problem I am having is catching the output and placing it into ${MD5PASS}. When I run it from command line I get this output..

[root@centbox trunk]# echo "password" | md5sum
286755fad04869ca523320acce0dc6a4  -

But I only need to grab the MD5. Any help would be great, as always I know I can get the best answers here with the level of knowledge amongst all of the experts. Thanks again
0
Sam Cohen
Asked:
Sam Cohen
  • 7
  • 5
1 Solution
 
woolmilkporcCommented:
Hi,
what you need is
MD5PASS=$(echo  ${DBPASS} | md5sum)
wmp
0
 
woolmilkporcCommented:

Explanation>
The $([command]) construct is the equivalent to the old, ugly backticks ( `[command]`)  thing.
It opens a subshell, executes the command in it and gets itself replaced by the results of the command, so it can easily be assigned to a variable.
Hope I could help
wmp
 
0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
Here is the exact code I am using: (not all of the code, but for this portion - the script is almost 300lines long)

read -p "Enter Admin Password: " PASSWD

MD5PASS="$(echo  ${DBPASS} | md5sum) | echo ${MD5PASS}"

mysql -uadmin -p`cat /etc/psa/.psa.shadow` ${DBNAME} -e"INSERT INTO jos_users VALUES (62, 'Administrator', 'admin', 'your-email@email.com','$MD5PASS',    'Super Administrator', 0, 1, 25, '2005-09-28 00:00:00', '2005-09-28 00:00:00', '','');" | tee -a ${LOG}

When I check the database this is what I get
424b5395fb6b509ab9cda199a1ed201b  - | echo
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
I have also tried it without the quotes, and I get the same result.
0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
I created a test script with this code:

  read -p "Enter Admin Password: " PASSWD
  MD5PASS=$(echo  ${DBPASS} | md5sum)
  echo ${MD5PASS}

When I run it I get this output:
[root@centbox trunk]# ./md5test
Enter Admin Password: test
68b329da9893e34099c7d8ad5cb9c940 -
[root@centbox trunk]#
0
 
woolmilkporcCommented:
Isn't that what you wanted?
wmp
0
 
woolmilkporcCommented:
Besides that, you prompt for PASSWD and then use DBPASS in the next step.
That's not correct.
wmp
0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
Yes, I just noticed that as well. Even when corrected it still dumps as 68b329da9893e34099c7d8ad5cb9c940 -

I need it to dump as

68b329da9893e34099c7d8ad5cb9c940

without the dash at the end. Sorry I didnt clarify
0
 
woolmilkporcCommented:
So you will have to issue
MD5PASS=$(echo  ${DBPASS} | md5sum | tr -d '-')
wmp
0
 
woolmilkporcCommented:
There is also a space before the dash.
To delete it too, Change tr-d '-' to tr -d ' -'   (means <space><dash>)
0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
Awesome! That is what I was looking for! thanks! Here is the final code which works great:


read -p "Enter Joomla Admin Password:" PASSWD
MD5PASS=$(echo -n ${PASSWD} | md5sum | tr -d '-')
echo ${MD5PASS}

Open in new window

0
 
woolmilkporcCommented:
You're welcome! Glad I could help you.
Norbert
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now