0

i have this code in my script :

check() { if [ $? -eq 0 ] then DATA=`date +%Y%m%d_%H.%M.%S` echo "O script $DATA Executou com sucesso" >>$log else DATA=`date +%Y%m%d_%H.%M.%S` echo "O script $DATA Executou com erro"; >>$log fi } 

but i want to put it in another script and when i execute in crontab once every 15 minutes. I want to check if the script executed successfully or not.

My question is in this first if section, how can I put the script ex3.sh for example

 if script ex3.sh sucess then DATA=`date +%Y%m%d_%H.%M.%S` echo "O script $DATA Executou com sucesso" >>$log else DATA=`date +%Y%m%d_%H.%M.%S` echo "O script $DATA Executou com erro"; >>$log fi } 
5
  • Please clarify your question, it is a little confusing. Commented Apr 17, 2020 at 1:55
  • To clarify, are you asking how to check if a certain script execution was successful or not in Bash? Commented Apr 17, 2020 at 6:00
  • yes, and i didn't no , i just know how to see if a function are success or not Commented Apr 17, 2020 at 8:04
  • I voted to close this question, because the OP until now, despite being asked several times, refuses to provide an exact definition of this problem. Commented Apr 17, 2020 at 8:14
  • i didnt know how do define more.. i'm new... Commented Apr 17, 2020 at 8:22

1 Answer 1

2

First to the terminology: What you show in your example (check) is not a script, but a function. It becomes a script, if you would add an actual invocation to check.

But to your proper question: You need to define somehow to the caller, whether or not you are successful. This can be done by havin your function return an exit code. This is done using the return statement. The convention is tor return 0 for success, and something between 1 and 127 for failure.

Hence, if you detect an error, do a

return 1 

in your script. If everything goes well, exit it by

return 0 

If you keep to this convention, you can indeed make use of the if statement you had in mind:

if check ARGUMENTS then .... # check function successful else .... # check function not successful fi 

The same principle does not only apply to functions, but also to scripts, with the main difference that with scripts, you signal the exit code not with return, but with an exit statement, i.e.

exit 0 # Success exit 1 # Failure 
Sign up to request clarification or add additional context in comments.

12 Comments

yes i know that, but i want to write a script with this.. but my problem is the first if, how can i check if another script, like : ex3.sh run ... i do this check in my main script, but he only check the function before... not the entire script..
When you design a piece of program - be it a whole program (script) or a single function -, you also design how an error condition is reported back to the caller. It does not make sense to ask for an arbitrary piece of code, whether it is successful or not, because the concept of what exactly does success mean, needs to be defined first.
yes i understand, but in this exercise, i just want to put in another script , if the first script run with sucess or not... in m code i put in the same script, but i want to create another and call the first script to see if runs all
You still have not defined, what "success" means.
only check the function before... not the entire script. do you mean you need to check the validity of a script before running it?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.