0

I have this code but I don't understand it very well.

Can you explain to me how the finish variable gets the value.

what does this assignment "finish=$?" mean?

or if they have any documentation to help me understand.

Thanks in advance.

#!/usr/bin/bash

/usr/bin/java -jar Report.jar $user $pass $base $dateini $datefin $iphost >> file.log 2>&1

finish=$?

if [ $finish -eq 0 ]; then
  echo "Report executed successfully" >> file.log
  exit 0
else
  echo "There was an error in the report" >> file.log
  exit 1
fi
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    `$?` is the exit code of the previous command executed, in your case `java -jar ...`. Usually an exit code of 0 represents success while a different one represents an error – Aaron Jun 17 '22 at 23:03
  • Does this answer your question? [Meaning of $? (dollar question mark) in shell scripts](https://stackoverflow.com/questions/7248031/meaning-of-dollar-question-mark-in-shell-scripts) – pjh Jun 17 '22 at 23:27
  • See [Why is testing "$?" to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/q/36313216/4154375). – pjh Jun 17 '22 at 23:28

1 Answers1

2

The exit status of the last command you ran (/usr/bin/java -jar ...) is stored in the special parameter $?.

Commands executed by the shell script or user have a exit status being either 0 (meaning the command was successful without errors), or a non-zero (1-255) value indicating the command was a failure.

Check here for further information

HatLess
  • 10,622
  • 5
  • 14
  • 32