Linux assign result of eval to variable

Hi there,

I need to run a command inside a bash script using the eval command and assign the results to a variable.  Here is what I have tried, but nothing has worked, any ideas?

eval "$md5original="md5sum /data/oracle/devpdb/dpdump/ONEGEO20171214.DMP | awk '{ print \$1 }'""
eval "md5sum /data/oracle/devpdb/dpdump/ONEGEO20171214.DMP | awk '{ print \$1 }'" &>> md5original

Thanks!
- Christian
Christian PalaciosSenior IT Systems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gabriel OrozcoSolution ArchitectCommented:
It is simple. You only need to execute the command. But to capture the output there are two ways.

The modern one, is enclosing the command in $()

Like in

DATE=$(date +%Y%m%d)

The other way is using backticks:


DATE=`date +%Y%m%d`

The latter is less clear and is slowly getting less used.
0
joolsCommented:
This has been answered above but to expand a little more;

md5original=$(md5sum /data/oracle/devpdb/dpdump/ONEGEO20171214.DMP | awk '{ print \$1 }')
echo ${md5original}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simon3270Commented:
Just a minor point, but in
    md5original=$(md5sum xxxx | awk '{print $1}'
there shouldn't be a backslash before the $ after the "print" statement.

To clarify you original command a little, the "eval" command is used to apply command-line processing to a line twice. The first as part of normal processing, and the second to re-interpret the resulting text.
So for example, if you have varname=b and b=5, then
   eval c=\$$varname
would first replace $varname with "b", and leave the first "$" alone because it is escaped - the backslash is removed as part of this processing. The resulting line is
    c=$b
so the shell processes this, and assigns 5 (the value of $b) to the variable $c.
0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
@Christian Palacios

Always use full path while using any Linux (/bin/awk or /bin/awk.exe or /bin/gawk.exe /bin/md5sum ...) oriented command.
Example error when missing full path:
$ unset -f md5sum
$ md5sum ()
{
        echo Always use full path for md5sum
        ErrorReturn=12
        return $ErrorReturn
}
$ md5sum murugesandins
Always use full path for md5sum
$ echo $?
12

Open in new window


md5original=$(/bin/md5sum /data/oracle/devpdb/dpdump/ONEGEO20171214.DMP | /bin/awk '{ print $1 }')
#You can also use:
md5original=''`/bin/md5sum /data/oracle/devpdb/dpdump/ONEGEO20171214.DMP | /bin/awk '{ print $1 }'`''
# or
md5original=''`/bin/md5sum /data/oracle/devpdb/dpdump/ONEGEO20171214.DMP | /bin/awk '{ printf( "%s", $1); }'`''
# or
md5original=''`/bin/md5sum /data/oracle/devpdb/dpdump/ONEGEO20171214.DMP | /bin/awk '{
        printf( "%s", $1);
}'`''
# or
md5original=''`/bin/md5sum /data/oracle/devpdb/dpdump/ONEGEO20171214.DMP | /bin/sed "s/ .*//;"`''
# Other ways are there using /usr/bin/tr /usr/bin/head /usr/bin/tail But that way will create performance issue while using more piping.

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

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.