Solved

catch md5sum output

Posted on 2008-10-24
12
1,318 Views
Last Modified: 2013-12-26
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
Comment
Question by:Sam Cohen
  • 7
  • 5
12 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Hi,
what you need is
MD5PASS=$(echo  ${DBPASS} | md5sum)
wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility

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
 
LVL 8

Author Comment

by:Sam Cohen
Comment Utility
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
 
LVL 8

Author Comment

by:Sam Cohen
Comment Utility
I have also tried it without the quotes, and I get the same result.
0
 
LVL 8

Author Comment

by:Sam Cohen
Comment Utility
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
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Isn't that what you wanted?
wmp
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Besides that, you prompt for PASSWD and then use DBPASS in the next step.
That's not correct.
wmp
0
 
LVL 8

Author Comment

by:Sam Cohen
Comment Utility
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
So you will have to issue
MD5PASS=$(echo  ${DBPASS} | md5sum | tr -d '-')
wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
There is also a space before the dash.
To delete it too, Change tr-d '-' to tr -d ' -'   (means <space><dash>)
0
 
LVL 8

Author Comment

by:Sam Cohen
Comment Utility
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
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
You're welcome! Glad I could help you.
Norbert
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigDiff challenge 17 74
firstChar challenge 13 82
unix example issues 18 43
Linux  Script File Attributes 5 0
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now