1

This question already has an answer here:

I'm trying to write a shell script in RHEL which will execute grub-md5-crypt and the user will type their password.

Now the problem is how can I grab the encrypted md5 hash displayed to the user in the shell script?

I tried to figure this out but command redirection will not work here. So how can I get the md5 encrypted text in the shell script after the script has executed grub-md5-crypt?

marked as duplicate by Gilles bash Sep 17 '14 at 20:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4
result=$(grub-md5-crypt | grep xy)
echo $result

If grub-md5-crypt prints to stderr use:

result=$(grub-md5-crypt 2>&1 | grep xy)
echo $result
  • It workes if I directly execute it in the shell. But not in my shell script. – Tarun Sep 17 '14 at 16:03
  • @Tarun This works in all shells. If it doesn't work for you, you made some other mistake, or you have an unrelated problem. Ask a new question if you can't figure it out, and be sure to post your code and say how it doesn't work (do you get errors, what happens, etc.). – Gilles Sep 17 '14 at 20:27
  • thank you for mentioning 2>&1 for getting result from stderr – kimbaudi Jul 17 at 8:26
2

If it doesn't work in your shell script you might want to use bash. Just add:

#!/bin/bash

It must be in the first line of your file!

This means your script will be using bash interpreter other than normal shell's one (/bin/sh).

Completing noEntry's answer, you can also save output to a file.

grub-md5-crypt | grep xy > output

Or:

result=$(grub-md5-crypt 2>&1 | grep xy)
echo $result > output

This will generate a file names output with whatever would have been printed on screen.

Not the answer you're looking for? Browse other questions tagged or ask your own question.