Page 1 of 1

how to get exit status of last command

Posted: Mon Oct 27, 2014 2:01 am
by csfreebird
Hi,
In bash, using echo $? to get the last command's exit status. But how to get it in newlisp?
e.g.
In bash, I get 0, it means the /user/chenshu folder exists in HDFS

Code: Select all

hdfs dfs -test -e /user/chenshu
echo $?
0
In newlisp, I have to get it using following way:

Code: Select all

> (exec "hdfs dfs -test -e /user/chenshu;echo $?")
14/10/27 09:19:33 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicab\
le
("0")
But I have to cut the warning message before ("0"), it's strange and not convenient.
Any way to get the exist status quickly?

Re: how to get exit status of last command

Posted: Mon Oct 27, 2014 5:16 am
by ralph.ronnquist
Not sure if its convenient enough, but (! ..) returns the process status, and prints sub process output. So, you might discard that on the command, and do:

Code: Select all

(! "hdfs dfs -test -e /user/chenshu > /dev/null 2>&1")
That only gives the return status.

Re: how to get exit status of last command

Posted: Mon Oct 27, 2014 2:14 pm
by csfreebird
I find an easy way to let exec return the correct status of command,
I wrote a newlisp script file for testing:

Code: Select all

#!/usr/bin/newlisp

(set 'x (exec "hdfs dfs -test -d /user/chenshu;echo $?"))
(println "x: " x)
(exit)
The output is:
x: ("0")

Re: how to get exit status of last command

Posted: Mon Oct 27, 2014 2:22 pm
by csfreebird
ralph.ronnquist wrote:Not sure if its convenient enough, but (! ..) returns the process status, and prints sub process output. So, you might discard that on the command, and do:

Code: Select all

(! "hdfs dfs -test -e /user/chenshu > /dev/null 2>&1")
That only gives the return status.
Thanks. But your way cannot be applied to all cases.
e.g.

Code: Select all

(println (! "hdfs dfs -test -s /user/chenshu > /dev/null 2>&1"))
It outputs 256. But the command in bash outputs 1

Code: Select all

[chenshu@hadoopMaster ~]$ hdfs dfs -test -s /user/chenshu
14/10/27 22:17:48 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicab\
le
[chenshu@hadoopMaster ~]$ echo $?
1

Re: how to get exit status of last command

Posted: Mon Oct 27, 2014 9:49 pm
by ralph.ronnquist
csfreebird wrote:
ralph.ronnquist wrote:Not sure if its convenient enough, but (! ..) returns the process status, and prints sub process output. So, you might discard that on the command, and do:

Code: Select all

(! "hdfs dfs -test -e /user/chenshu > /dev/null 2>&1")
That only gives the return status.
Thanks. But your way cannot be applied to all cases.
e.g.

Code: Select all

(println (! "hdfs dfs -test -s /user/chenshu > /dev/null 2>&1"))
It outputs 256. But the command in bash outputs 1
Fair enough. The manual is comfortably unspecific about the return value of "!", and it probably is more portable to use your method:

Code: Select all

(int (last (exec (string cmd ";echo $?")))
A bit of experimenting indicates that on my machine (32-bit linux), "!" returns the command status up shifted 8 bits, which kind of is on a par with the "system" man page, if you read it upside down. It thus needs down shifting to recover the actual command return code:

Code: Select all

(>> (! cmd) 8)
However, this might not be portable.