I have a python script that constantly exits exit code 42. Question: how can I replace the exit code in bash with another one, while keeping the number for further processing?
#!/bin/bash -xe my_script.py I have a python script that constantly exits exit code 42. Question: how can I replace the exit code in bash with another one, while keeping the number for further processing?
#!/bin/bash -xe my_script.py once you use the -e option of the bash program, the command inside the script that returns a non-zero return code will stop further interpretation of your script. to ignore a nonzero return code, you can, for example, use the list operator || :
ΠΊΠΎΠΌΠ°Π½Π΄Π°-ΠΊΠΎΡΠΎΡΠ°Ρ-ΠΌΠΎΠΆΠ΅Ρ-Π²Π΅ΡΠ½ΡΡΡ-ΠΎΡΠΈΠ±ΠΊΡ || ΠΊΠΎΠΌΠ°Π½Π΄Π°-ΠΊΠΎΡΠΎΡΠ°Ρ-Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡΡ-Π²-ΡΡΠΎΠΌ-ΡΠ»ΡΡΠ°Π΅ if the ΠΊΠΎΠΌΠ°Π½Π΄Π°-ΠΊΠΎΡΠΎΡΠ°Ρ-ΠΌΠΎΠΆΠ΅Ρ-Π²Π΅ΡΠ½ΡΡΡ-ΠΎΡΠΈΠ±ΠΊΡ returns a non-zero return code, then the ΠΊΠΎΠΌΠ°Π½Π΄Π°-ΠΊΠΎΡΠΎΡΠ°Ρ-Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡΡ-Π²-ΡΡΠΎΠΌ-ΡΠ»ΡΡΠ°Π΅ will be executed, and if this second command returns the zero return code, the script will continue to be interpreted in "normal mode".
The simplest example for the second command is this : (colon, see $ help : for details). it does nothing, and ends with a zero return code.
such a construction will not lead to the termination of interpretation when using the -e option, even if the first command returns a non-zero return code:
ΠΊΠΎΠΌΠ°Π½Π΄Π°-ΠΊΠΎΡΠΎΡΠ°Ρ-ΠΌΠΎΠΆΠ΅Ρ-Π²Π΅ΡΠ½ΡΡΡ-ΠΎΡΠΈΠ±ΠΊΡ || : since you need to save the return code of the βproblemβ command, the most logical is on the right side of the operator || perform the assignment of the return code of the last command executed (stored in the $? variable) to some variable (for example, $result ):
ΠΊΠΎΠΌΠ°Π½Π΄Π°-ΠΊΠΎΡΠΎΡΠ°Ρ-ΠΌΠΎΠΆΠ΅Ρ-Π²Π΅ΡΠ½ΡΡΡ-ΠΎΡΠΈΠ±ΠΊΡ || result=$? assignment operator result=$? always successful, because the interpretation of the script will not be interrupted.
total:
#!/bin/bash set -e result=0 my_script.py || result=$? echo "ΠΊΠΎΠ΄ Π²ΠΎΠ·Π²ΡΠ°ΡΠ° ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ½ΠΎΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ: $result" In the piggy bank.
In bash, there is already an internal array variable, which is like the status of a conveyor or a single executed command:
PIPESTATUS measure:
~$ ls -la | grep emacs drwx------ 3 u0_a207 u0_a207 4096 Sep 4 19:58 .emacs.d ~$ echo ${PIPESTATUS[@]} 0 0 Links
PIPESTATUS may be useful, but it is not clear how this answers the current question. If you find something interesting (βhow to get the status of teams in the pipeline to get in the bashβ) and want to share with others (or the future yourself), you can ask and answer the question yourself - this is clearly welcomed - jfsSource: https://ru.stackoverflow.com/questions/714514/
All Articles