how to get exit status of last command

Q&A's, tips, howto's
Post Reply
csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

how to get exit status of last command

Post 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?

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: how to get exit status of last command

Post 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.

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: how to get exit status of last command

Post 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")

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: how to get exit status of last command

Post 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

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: how to get exit status of last command

Post 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.

Post Reply