2012-08-02 113 views
-3

我已經完成了一個shell腳本,該腳本獲取.jar文件的輸出並將其分配給變量。如何獲取perl腳本中子例程的返回值bash

rewards_generator.sh

APP_ROOT=/home/testApps/ 
JAR=${APP_ROOT}ClusterGenerator/generator.jar 

#get clusters 
clusters=$(loadClusters $1) 

for i in `echo $clusters | sed 's/,/ /g'` 
do 
    #pull cluster records from database and save query return status to $x 
    x=$(/usr/IBM/WebSphere/ProcServer/java/bin/java ${JAR} ${i/-/_} 2>&1) 
done 

基本上,我已經做了Java應用程序是System.out.print查詢返回的狀態。然後在bash中使用2>&1以便進入輸出流並將該值賦給shell腳本變量。

現在我該如何獲取perl腳本的返回值並將其分配給shell腳本變量?它與我上面所做的一樣嗎?還是有其他方法可以做到這一點?

+3

你試過了嗎? – 2012-08-02 04:13:36

+0

我還沒有嘗試過。 – makalshrek 2012-08-02 04:55:07

+0

'2&1'不需要。 'System.out'已經*了* stdout,或者是fd 1. – 2012-08-02 08:52:36

回答

1

您可以使用反引號將外部命令的輸出記錄在bash腳本中。

這裏有一個簡單的例子:

#!/bin/bash 

# Execute the script, recording output to a variable 
x=`/path/to/script.pl` 

# Display or act on the output some time later 
echo "script output: $x" 
1

現在,我怎樣才能得到一個perl腳本的返回值,並將其賦值給一個shell腳本變量?

#! /bin/bash 
perl script.pl 
return_value=$? 
相關問題