For example
$ true | false | true
Or
$ ls -S1 hoge* | haed -n1
Immediately after running a shell script that includes pipe processing like
$ echo $?
When checking the status code with a command such as, even if an error occurs in the first or intermediate process, only the last status code of the pipe can be obtained.
When you get the status code of the progress, you can get it by referring to the array $ {PIPESTATUS [@]}
immediately after execution.
$ true | false | true
$ echo ${PIPESTATUS[@]}
0 1 0
Normally, in the case of Fabric, the return values of the run
command and sudo
command have properties such as .succeeded
and .failed
, so they are often referred to.
result = run("false", warn_only=True)
print result.succeeded
However, since there is no variable corresponding to $ {PIPESTATUS [@]}
, the status code cannot be obtained in the middle.
Consider an example like the shell script at the beginning.
@task
def return_test():
#Show the largest file containing the string hoge in the current directory
result = run("ls -S1 *hoge* | head -n1")
print result.succeed
print result
At this time, if there is no file / directory containing hoge
in the current directory, .succeeded
will only return the result of run
(since head can be executed, this is True
), As the content, the error display (standard error output) is returned as it is.
[192.168.0.1] Executing task 'return_test'
[192.168.0.1] run: ls -S1 *hoge* | head -n1
[192.168.0.1] out: ls: cannot access *hoge*: No such file or directory
[192.168.0.1] out:
True
ls: cannot access *hoge*: No such file or directory
Done.
Disconnecting from 192.168.0.1... done.
The information I originally wanted wasn't returned, and it's unpleasant, but in this example, it may happen that the directory name is such a directory name.
Let's make the tea muddy.
@task
def return_test():
result = run("ls -S1 *hoge* 2> /dev/null | head -n1")
print result
[192.168.0.1] Executing task 'return_test'
[192.168.0.1] run: ls -S1 *hoge* 2> /dev/null | head -n1
Done.
Disconnecting from 192.168.0.1... done.
Recommended Posts