Raise Exception on Shell Command Failure

Raise exception on shell command failure?

Easiest way would be to create a new function (or redefine an existing one) to call system() and check the error code.

Something like:

old_sys = system

def system(...)
old_system(...)
if $? != 0 then raise :some_exception
end

This should do what you want.

Raise error in a Bash script

This depends on where you want the error message be stored.

You can do the following:

echo "Error!" > logfile.log
exit 125

Or the following:

echo "Error!" 1>&2
exit 64

When you raise an exception you stop the program's execution.

You can also use something like exit xxx where xxx is the error code you may want to return to the operating system (from 0 to 255). Here 125 and 64 are just random codes you can exit with. When you need to indicate to the OS that the program stopped abnormally (eg. an error occurred), you need to pass a non-zero exit code to exit.

As @chepner pointed out, you can do exit 1, which will mean an unspecified error.

Raise exception on shell command failure?

Easiest way would be to create a new function (or redefine an existing one) to call system() and check the error code.

Something like:

old_sys = system

def system(...)
old_system(...)
if $? != 0 then raise :some_exception
end

This should do what you want.

IPython - Raise exception when a shell command fails

As of IPython 4.0.1, !cmd is transformed into get_ipython().system(repr(cmd)) (IPython.core.inputtransformer._tr_system()).
In the source, it's actually InteractiveShell.system_raw(), as inspect.getsourcefile() and inspect.getsource() can tell.

It delegates to os.system() in Windows and subprocess.call() in other OSes. Not configurable, as you can see from the code.

So, you need to replace it with something that would call subprocess.check_call().

Apart from monkey-patching by hand, this can be done with the IPython configuration system. Available options (viewable with the %config magic) don't allow to replace TerminalInteractiveShell with another class but several TerminalIPythonApp options allow to execute code on startup.

Do double-check whether you really need this though: a look through the system_raw()'s source reveals that it sets the _exit_code variable - so it doesn't actually fail completely silently.

Catching exception of a shell command in Perl 6

shell doesn't throw the Exception until the sink.

The try block with only the shell in it fully executes without an exception being thrown, returning the last value in the block, which then gets sunk outside the context of the try, which then throws the Exception.

You can see this with:

put "start";
try {
shell "./prog.sh";
'something';
}
put "end";

Now the shell gets sunk inside the try, which gets caught by the implicit CATCH of the try. The try block returns the last value in the block, the 'something', which then gets safely sunk outside the try.

You can also force the sink to happen inside the try:

put "start";
try {
sink shell "./prog.sh"
}
put "end";

Your added CATCH block is just preventing the try block from returning the return value from the shell.

You can re-arrange them and see that this still blows up:

put "start";
try {
CATCH { default {} }
shell "./prog.sh";
}
put "end";

The best, most clear way to handle this IMHO would be to check the return from the shell yourself rather than letting it sink and throw the Exception:

put "start";
if shell "./prog.sh" {
say 'ok'
}
else {
say 'failed'
}
put "end";

How to purposely throw an Error in the shell script

but i need something else instead of -cd 123 syntax

You could say:

exit 42

instead. This would make the script exit with a non-zero exit code that should be picked up by Jenkins.

help exit tells:

exit: exit [n]
Exit the shell.

Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.

raise error if files output some error (without raising shell error)

The purpose of [[ ... ]] is to generate an exit code that if, &&, or || can test. grep already does that. You don't care about grep's output, and you can suppress it with the -q option.

echo "abcdef" | grep -q "f" && echo "error"

Note that your function is wrapping the pipeline, but you have removed the echo command. You need to put that back. (Actually, you should use printf instead of echo for portability and predictability. And finally, error messages should be written to standard error, not standard output.

test_if_exe_fails(){
print '%s' "$1" | grep -q "$2" && echo "error: $2" >&2 && exit 1
}

test_if_exe_fails "$(myApp.exe)" "myerror message"

One last improvement: rather than capture the output of myApp.exe, just pipe it directly to the function:

test_if_exe_fails(){
grep -q "$1" && echo "error: $1" >&2 && exit 1
}

myApp.exe | test_if_exe_fails "myerror message"

Capture shell output after running python script that raised Exception

Python writes exceptions to standard error, hence if you want to capture them along with the normal output, you should redirect stderr to stdout by adding 2>&1 to the bash input.

In your case it would be bash("""python3 some_script.py 2>&1""").

I assume the bash function sends the argument to the bash shell.



Related Topics



Leave a reply



Submit