0

Can someone convert this code? I got this from the web.

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
1
  • 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? Commented Oct 11, 2013 at 1:06

2 Answers 2

1

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
0

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.