Storing command output in a variable and using cut/awk

Hi,

My aim is to get the md5 hash of a file and store it in a variable.

var1="md5sum file1"
$var1

The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1

i just need to get the first part and put it into a variable.

var1="md5sum file1"
$var1 | awk '{print $1}'

This only prints the first part which is what i want but i dont know how to put it into a variable to use later on

newvar={$var1 | awk '{print $1}'}

or

newvar=`$var1 | awk '{print $1}'`

hi,

the first one doesn't work , shows a syntax error on the last bracket.

I am doing this.

var1="md5sum file1"

newvar={$var1 | awk '{print $1}'}

$newvar

I'm sorry, the first command should be:

newvar=$($var1 | awk '{print $1}')
1 Like

JustALol,
I think you are trying to over work this and have confused it trying to make it neat. Would this do?:-

md5sum file1 | read newvar filename
print $newvar

What is the intention with this? If you are looking to store the value long term and then check for unauthorised changes, then md5sum allows you to read a file containing the hashes and compare them:-

echo "Original file" > my_test_file
for file in *
do
   md5sum $file
done > /tmp/my_hashes

echo "Changed file" > my_test_file
md5sum -c /tmp/my_hashes

Does that work with what you are really intending to do?

Robin

Hi,

sorry for my late reply, it ended up working fine

newvar=$($var1 | awk '{print $1}')

Thankyou for your replies