-
Notifications
You must be signed in to change notification settings - Fork 62
Simple mode
In simple mode you execute commands in a blocking way. That means that execution of
`ping -n 4 8.8.8.8
will not return until it is finished. When it's done a Result of the execution becomes available to you. You can assign the Result to your variable like this:
result = `ping -n 4 8.8.8.8
###Result
Result is a class that provides several properties to you:
stdout property returns the whole text of the command stdout to you.
stderr the same but for stderr.
stdout_lines is a list of all stdout lines. It may be convenient if you want to iterate over all lines or access some specific line by index. Lines in this list do not contain the line separator at the end.
stderr_lines the same but for stderr.
returncode return code of the command executed
###Syntactic sugar
You can easily check that return code is zero like this
result = `ls -l somedir
if result:
print('Return code is 0')
else:
print('Return code is not zero')
It is possible to iterate over lines of result
result = `ls -l somedir
for line in result:
print line.upper()
Compaire two results (by text)
`echo 1` == `echo 1` # True
`echo 1` == `echo 2` # False
Extract stdout from result
result = `ls -l somedir
print result
The latest is equivalent to print str(result) or print result.stdout
###Experimental mode
Experimental mode is a behavior of shellpy that maybe will be default in the future, but not yet. In the experimental mode every non zero return code causes an exception NonZeroReturnCodeError to be raised. User in this mode is supposed to handle errors not with IF but with EXCEPT. Rationale for this is that most likely people do not handle errors for all lines of shell code and there is not way to make them handle it like in Go. Thus it is easy to skip an error in shell and continue execution with it not even noticing it without verbose output turned on. The mode with exception solves this problem well. It is a python way and makes you see all the errors.
To run scripts in experimental mode simply add -x parameter to shellpy like shellpy -x my.spy. If you use shellpython directly from python, then set config.EXIT_ON_ERROR to True, it will have the same behavior.
If the exception was not handled by user, the script will stop working and print stacktrace, returncode and stderr of the failed command.