Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Can someone convert this code? I got this from the web. I am new on this and don't know how eval works.

Originally, there is a parameter from a command that is either a plus (+) sign or a minus (-) sign. But since I only need now the addition (its not in the parameter anymore), I want this to do addition automatically.

-|+) eval '(('jd2=${jd1}${5}${6}'))'
jd2date $jd2

Pardon if the way I am asking is confusing.

share|improve this question

closed as unclear what you're asking by Sukminder, Anthon, slm, jasonwryan, Joseph R. Oct 11 '13 at 20:58

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Convert it to what? Could you give us an example input and desired output? What shell are you using? What operating system? What's jd2date? What is in $jd2? –  terdon Oct 11 '13 at 1:06

2 Answers 2

Does the code look like this?

case "$5" in
    -|+) eval '(('jd2=${jd1}${5}${6}'))' jd2date $jd2 
esac

If so, you might call the command like so:

script arg1 arg2 arg3 arg4 + 6

or

script arg1 arg2 arg3 arg4 - 10

You may be able to make the code look like this:

 eval '(('jd2=${jd1}+${5}'))' jd2date $jd2 

The command would be called like so:

script arg1 arg2 arg3 arg4 10

${5}, ${6}, ${jd1}, and $jd2 are substituted with the contents of their respective variables. Variables that are numbers (e.g. $5 and $6) are positional parameters.

Since you no longer need the operator to be placed by a variable, the call to eval is not necessary. Your code may look like this:

 (( jd2=${jd1}+${5} ))
 jd2date $jd2

Or, with slightly cleaner syntax:

 (( jd2 = jd1 + $5 ))
 jd2date $jd2
share|improve this answer

I'm not sure what you're trying to achieve with the code fragment, but here's an example of the use of eval along with a comparison with similar command substitution operators:

$ echo `echo 2 + 2 | bc`
4
$ echo $(echo 2 + 2 | bc)
4
$ eval "echo 2 + 2 | bc"
4
$ echo "date"
date
$ eval "date"
Thu Oct 10 21:20:01 EDT 2013

So eval "evaluates" a command, or series of commands, and returns the result of running that command. For the life of me, I can't figure out any use for eval beyond that which can be done by either running the commands directly or using the backquote or $(...) syntax.

share|improve this answer