how to get exit status of last command

Started by csfreebird, October 26, 2014, 07:01:03 PM

Previous topic - Next topic

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

hdfs dfs -test -e /user/chenshu
echo $?
0


In newlisp, I have to get it using following way:

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

rrq

#1
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:
(! "hdfs dfs -test -e /user/chenshu > /dev/null 2>&1")
That only gives the return status.

csfreebird

#2
I find an easy way to let exec return the correct status of command,

I wrote a newlisp script file for testing:

#!/usr/bin/newlisp

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


The output is:

x: ("0")

csfreebird

#3
Quote from: "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:
(! "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.

(println (! "hdfs dfs -test -s /user/chenshu > /dev/null 2>&1"))


It outputs 256. But the command in bash outputs 1



[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

rrq

#4
Quote from: "csfreebird"
Quote from: "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:
(! "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.

(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:
(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:
(>> (! cmd) 8)
However, this might not be portable.