![](/img/trans.png)
[英]How to get the java program completion status from unix shell script?
[英]How to callback process status from a PHP script,Java program to the calling bash shell
簡單來說
我有一個PHP和一個Java程序,其中一個程序是從我的shell調用的,具體取決於條件。 這兩個程序基本上是相似任務的兩個路徑-一種路徑用於一種文件格式,另一種路徑用於另一種文件格式。 我希望這兩個路徑返回狀態(成功或失敗),然后讓外殼讀取回調並相應地執行一些通用邏輯。
描述
這是一個代表 -
|
|
\/
Shell logic
|
|
______________
| check a |
| condition |
______________
| |
/\ / \
/ \ / \
|Yes| |No |
\ / \ /
\/ \/
| |
| |
\/ \/
run PHP run Java
script program
| |
| |
\/ \/
callback callback
status status
| |
\/ \/
------------------
| Detect callback |
| status in shell |
-------------------
|
|
\/
Do something
我調用這樣的程序 -
php $dbx_parser_script_path/cli_complete_dbx_parser.php input_dbx_file=$input_file package_root=$package_root
和
java -classpath $package_root/some/jar/file:$package_root/examples/MessagesAndAttachments/src/ Example $input_file $package_root
那么,有兩件事 -
請注意,兩個程序在運行時都會回顯一些消息。 因此,某種閱讀打印輸出不能是一個干凈的解決方案。 如果有某種適當的方式可以讀取外殼程序中已執行程序的返回值,並且這兩種語言(Java和PHP)都可以將某些值返回給調用者腳本,那么它就是我要尋找的。
謝謝
更新
固定但這個奇怪的東西與$?
//after php script execution command - my php script ends with exit(1)
temp=$?
echo "See "$?" "$temp
echo "with ?"
if [ $? -eq 1 ]; then
echo "success"$?
else
echo "failure"$?
fi
echo "with temp"
if [ $temp -eq 1 ]; then
echo "success"$temp
else
echo "failure"$temp
fi
輸出是-
See 0 1
with ?
failure1
with temp
success1
為什么$?
的價值$?
走開。 為什么需要將其存儲在另一個變量中?
在您的java程序中,您可以返回退出狀態,如下所示:
//return exit status of 1
System.exit(1);
同樣,在PHP中:
//exit with an error code of 1
exit(1);
在shell腳本中,您可以檢查使用$?
執行的最后一個命令的退出狀態$?
例:
#run java command
java Main args
#the exit status of the previous command is in `$?`. So check it:
if [ $? -eq 1 ]
then
# do something
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.