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?
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.
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")
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
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.