gpt4 book ai didi

java - shell脚本没有完全从java执行

转载 作者:行者123 更新时间:2023-11-30 07:45:40 24 4
gpt4 key购买 nike

我有一个执行 sql 构建工作的 shell 脚本。

当从 unix 提示符手动调用它时,它正在执行并给出预期的结果。

但是当从 java 调用相同的方法时,它没有完全执行。

手动执行:

MODIFY_PATCH_CREATION.ksh 118765 CP_14052906_28112015134449_16

日志文件显示为:

Installing Patch for BATCH ID:118765 and PATCH NAME:CP_14052906_28112015134449_16
CP_14052906_28112015134449_16_RELEASE directory has been created
CP_14052906_28112015134449_16_INSTALL directory has been created
CP_14052906_28112015134449_16_ROLLBACK directory has been created
SQL and LOG directories have been created for INSTALL
INSTALL script is created
SQL and LOG directories have been created for ROLLBACK
ROLLBACK script is created
SQL files are created for INSTALL
SQL files are created for ROLLBACK
Patch installed Successfully..

但是如果我从 java 程序中执行相同的操作,如下所示:

java代码:

public class ShellTest{


public static void main(String a[]) throws Exception{


System.out.println("Hello kp");
Runtime.getRuntime().exec("MODIFY_PATCH_CREATION.ksh 118765 CP_14052906_28112015134449_16");


}

}

当作为 java ShellTest 执行时,日志仅在一定程度上出现

Installing Patch for BATCH ID:118765 and PATCH NAME:CP_14052906_28112015134449_16
CP_14052906_28112015134449_16_RELEASE directory has been created
CP_14052906_28112015134449_16_INSTALL directory has been created
CP_14052906_28112015134449_16_ROLLBACK directory has been created
SQL and LOG directories have been created for INSTALL
INSTALL script is created
SQL and LOG directories have been created for ROLLBACK
ROLLBACK script is created
SQL files are created for INSTALL

Shell脚本代码:

#!/bin/bash
BATCH_ID=$1
echo "BATCH_ID: $BATCH_ID"
PATCH_NAME=$2
echo "PATCH_NAME: $PATCH_NAME"

echo "Creating patch for batch id $BATCH_ID"

ARBOR_USR=`echo $K_CAT_USERNAME`
dateTime=`date '+%d%m%Y_%H%M%S'`

PATCH_DIR=$ARBORDIR/cw/CPF_GSA_Modify
RELEASE_DIR=$ARBORDIR/cw/CPF_GSA_Modify/$2_RELEASE
INSTALL_DIR=$ARBORDIR/cw/CPF_GSA_Modify/$2_RELEASE/$2_INSTALL/
ROLLBACK_DIR=$ARBORDIR/cw/CPF_GSA_Modify/$2_RELEASE/$2_ROLLBACK/

##Logfile creation for install
cd $PATCH_DIR
touch $2_PATCH_log_$dateTime.txt
log_file=$PATCH_DIR/$2_PATCH_log_$dateTime.txt
echo "Installing Patch for BATCH ID:$BATCH_ID and PATCH NAME:$PATCH_NAME" >>$log_file

mkdir $2_RELEASE
echo "$2_RELEASE directory has been created" >>$log_file

cd $2_RELEASE
mkdir $2_INSTALL
echo "$2_INSTALL directory has been created" >>$log_file
mkdir $2_ROLLBACK
echo "$2_ROLLBACK directory has been created" >>$log_file

cd $INSTALL_DIR
mkdir SQL
mkdir LOG
echo "SQL and LOG directories have been created for INSTALL" >>$log_file
cp $PATCH_DIR/vge_cpf_gsa_mod_db_install.ksh $2_install.ksh
sed -i "s/vge_cpf_gsa_mod_db/$2/g" $2_install.ksh
echo "INSTALL script is created" >>$log_file
#cd SQL
#touch sql_list.sql

cd $ROLLBACK_DIR
mkdir SQL
mkdir LOG
echo "SQL and LOG directories have been created for ROLLBACK" >>$log_file
cp $PATCH_DIR/vge_cpf_gsa_mod_db_rollback.ksh $2_rollback.ksh
sed -i "s/vge_cpf_gsa_mod_db/$2/g" $2_rollback.ksh
echo "ROLLBACK script is created" >>$log_file
#cd SQL
touch sql_list.sql

cd $INSTALL_DIR/SQL/
echo 'Going to Connect to database'
touch display.log
sqlplus $ARBOR_USR/`cat $ARBORDIR/.arborpw`@$ORACLE_SID << THEEND >>display.log
set define off;
set linesize 3000;
set head off;
set feedback off;

spool CW_NEW_MRC.sql;
SELECT 'UPDATE RATE_RC a SET INACTIVE_DATE= (SELECT DISTINCT active_date FROM CW_PC_RR b WHERE a.element_id=b.element_id AND a.component_id=b.component_id and rownum<2) WHERE ELEMENT_ID='||MEMBER_ID||' AND COMPONENT_ID= '||COMPONENT_ID||' AND INACTIVE_date IS NULL ;' STATEMENT FROM CW_OLD_MRC_RATES WHERE batch_id=$BATCH_ID;
select 'INSERT INTO "RATE_RC" ("RATE_CLASS", "ELEMENT_ID", "COMPONENT_ID", "EQUIP_TYPE_CODE", "EQUIP_CLASS_CODE", "CLASS_OF_SERVICE_CODE", "TYPE_ID_RC", "BILLING_FREQUENCY", "CURRENCY_CODE", "RATE", "JURISDICTION", "DISTANCE_INCREMENT", "UNITS_TYPE", "UNITS_LOWER_LIMIT", "UNITS_UPPER_LIMIT", "UNITS_RATE", "POP_LOWER_LIMIT", "POP_UPPER_LIMIT", "DISTANCE_RATE", "ADD_IMPLIED_DECIMALS", "DATE_CREATED", "ACTIVE_DATE", "INACTIVE_DATE", "TEST_FLAG", "FOREIGN_CODE")VALUES ( '''|| RATE_CLASS||''','''||ELEMENT_ID||''','''||COMPONENT_ID||''','''||EQUIP_TYPE_CODE||''','''||EQUIP_CLASS_CODE||''','''||CLASS_OF_SERVICE_CODE||''','''||TYPE_ID_RC||''','''||BILLING_FREQUENCY||''','''||CURRENCY_CODE||''','''||RATE||''','''||JURISDICTION||''','''||DISTANCE_INCREMENT||''','''||UNITS_TYPE||''','''||UNITS_LOWER_LIMIT||''','''||UNITS_UPPER_LIMIT||''','''||UNITS_RATE||''','''||POP_LOWER_LIMIT||''','''||POP_UPPER_LIMIT||''','''||DISTANCE_RATE||''','''||ADD_IMPLIED_DECIMALS||''','''||to_char(DATE_CREATED ,'dd-mon-yyyy')||''','''||to_char(ACTIVE_DATE , 'dd-mon-yyyy')||''','''||to_char(INACTIVE_DATE , 'dd-mon-yyyy')||''','''||TEST_FLAG||''','''||FOREIGN_CODE || '''); ' statement from CW_PC_RR where batch_id=$BATCH_ID;
spool CW_NEW_DISCOUNT.sql;
SELECT 'UPDATE RATE_DISCOUNT RDIS SET RDIS.DATE_INACTIVE= (SELECT DISTINCT date_active FROM CW_PC_RD b WHERE RDIS .discount_id=b.discount_id and rownum<2) WHERE DISCOUNT_ID='||CR.DISCOUNT_ID|| ' AND billing_frequency= '||billing_frequency||' AND currency_code= '||currency_code||' AND DATE_INACTIVE IS NULL;' STATEMENT FROM CW_CPF_MODIFY_DISCOUNT_RATES CR where batch_id=$BATCH_ID;
SELECT 'INSERT INTO "RATE_DISCOUNT" ("DISCOUNT_ID", "RATE_CLASS", "RANGE_ORIGIN", "RANGE_TERMINUS", "BILLING_FREQUENCY", "CURRENCY_CODE", "DISCOUNT_PERCENT", "IMPLIED_DECIMALS", "DISCOUNT_AMOUNT", "DATE_ACTIVE", "DATE_INACTIVE", "DATE_CREATED", "LOOKUP_THRESHOLD_TIER_ID", "QUALIFYING_THRESHOLD_TIER_ID") VALUES ('''||DISCOUNT_ID||''','''|| RATE_CLASS||''','''||RANGE_ORIGIN||''','''||RANGE_TERMINUS||''','''||BILLING_FREQUENCY||''','''||CURRENCY_CODE||''','''||DISCOUNT_PERCENT||''','''||IMPLIED_DECIMALS||''','''||DISCOUNT_AMOUNT||''','''||to_char(DATE_ACTIVE , 'dd-mon-yyyy')||''','''||'' ||''','''||to_char(DATE_ACTIVE , 'dd-mon-yyyy')||''','''||LOOKUP_THRESHOLD_TIER_ID||''','''||QUALIFYING_THRESHOLD_TIER_ID||''');' statement from CW_PC_RD where batch_id=$BATCH_ID;
spool CW_NEW_UNIT_CREDIT.sql;
SELECT 'UPDATE RATE_UNIT_CR RDIS SET RDIS.DATE_INACTIVE = (SELECT DISTINCT date_active FROM CW_PC_RUC b WHERE b.unit_cr_id=rdis.UNIT_CR_ID and rownum<2) WHERE UNIT_CR_ID='||CR.DISCOUNT_ID|| ' AND billing_frequency= '||billing_frequency||' AND DATE_INACTIVE IS NULL;' STATEMENT FROM CW_CPF_MODIFY_UNITCREDIT_RATES CR where batch_id=$BATCH_ID;
SELECT 'INSERT INTO "RATE_UNIT_CR" ("UNIT_CR_ID", "RATE_CLASS", "RANGE_ORIGIN", "RANGE_TERMINUS", "BILLING_FREQUENCY", "CURRENCY_CODE", "UNITS_FREE", "AMOUNT_FREE", "DATE_ACTIVE", "DATE_INACTIVE") VALUES ('''||UNIT_CR_ID||''','''||RATE_CLASS||''','''||RANGE_ORIGIN||''','''||RANGE_TERMINUS||''','''||BILLING_FREQUENCY||''','''||CURRENCY_CODE||''','''||UNITS_FREE||''','''||AMOUNT_FREE||''','''||to_char(DATE_ACTIVE , 'dd-mon-yyyy')||''','''||to_char(DATE_ACTIVE , 'dd-mon-yyyy')||''');' statement from CW_PC_RUC where batch_id=$BATCH_ID;
spool CW_NEW_TARIFF_RATE.sql;
SELECT 'UPDATE TARIFF_RATE TR SET TR.DATE_INACTIVE = (SELECT DISTINCT date_active FROM CW_PC_TR b where tr.tariff_id=b.tariff_id and tr.bundle_id=b.bundle_id and rownum<2) WHERE TARIFF_ID='||CR.TARIFF_ID||' AND BUNDLE_ID='||CR.BUNDLE_ID||' AND DATE_INACTIVE IS NULL;' STATEMENT FROM CW_OLD_TARIFF_SELF_OPT_RATES CR;
SELECT 'INSERT INTO "TARIFF_RATE" ("TARIFF_ID", "BUNDLE_ID","CHARGE_AMOUNT","CURRENCY_CODE","IMPLIED_DECIMALS","DATE_ACTIVE","DATE_CREATED") VALUES ('''||TARIFF_ID||''','''||BUNDLE_ID||''','''||CHARGE_AMOUNT||''','''||CURRENCY_CODE||''','''||IMPLIED_DECIMALS||''','''||DATE_ACTIVE||''','''||DATE_CREATED||''');' statement from CW_PC_TR where batch_id=$BATCH_ID;
SELECT 'UPDATE TARIFF_RATE_BAND TRB SET TRB.STEP='||DECODE(STEP,'','NULL',STEP)||',TRB.RANGE_ORIGIN='||DECODE(RANGE_ORIGIN,'','NULL',RANGE_ORIGIN)||',TRB.RANGE_TERMINUS='||DECODE(RANGE_TERMINUS,'','NULL',RANGE_TERMINUS)||' WHERE TRB.TARIFF_ID='||TARIFF_ID||' AND TRB.AGGREGATION_KEY='||AGGREGATION_KEY||' AND TRB.BUNDLE_ID='||BUNDLE_ID||';' STATEMENT FROM CW_NEW_TARIFF_SELF_OPT_RATES where batch_id=$BATCH_ID;
spool CW_NEW_USAGE_RATES.sql;
SELECT 'UPDATE RATE_USAGE RU SET RU.INACTIVE_DT= (SELECT DISTINCT active_dt FROM CW_PC_RU b where ru.component_id=b.component_id and ru.type_id_usg=b.type_id_usg and ru.jurisdiction=b.jurisdiction and rownum<2 ) WHERE SEQNUM='||SEQNUM||'AND INACTIVE_DT IS NULL;' STATEMENT FROM CW_OLD_USAGE_RATES WHERE batch_id=$BATCH_ID;
select 'INSERT INTO "RATE_USAGE" ("SEQNUM", "RATE_CLASS", "TYPE_ID_USG", "BILL_CLASS","JURISDICTION","POINT_CLASS_TARGET","POINT_CLASS_ORIGIN","UNITS_INDICATOR","PROVIDER_CLASS","RATE_PERIOD","CURRENCY_CODE","ELEMENT_ID","EQUIP_TYPE_CODE","EQUIP_CLASS_CODE","CLASS_OF_SERVICE_CODE","FIXED_CHARGE_AMT","ADD_FIXED_AMT","ADD_UNIT_RATE","ACTIVE_DT","INACTIVE_DT","CREATE_DT","CHANGE_DT","CHANGE_WHO","INCREMENTAL_BANDS","RATE_UNITS_TYPE","DISTANCE_BAND_ID","ZONE_CLASS","ADD_IMPLIED_DECIMAL","IS_DEFAULT_RATE","COMPONENT_ID","BILLING_UNITS_TYPE","MIN_CHARGE_AMT","ADD_MIN_AMT") VALUES ('''||SEQNUM||''','''||RATE_CLASS||''','''||TYPE_ID_USG||''','''||BILL_CLASS||''','''||JURISDICTION||''','''||POINT_CLASS_TARGET||''','''||POINT_CLASS_ORIGIN||''','''||UNITS_INDICATOR||''','''||PROVIDER_CLASS||''','''||RATE_PERIOD||''','''||CURRENCY_CODE||''','''||ELEMENT_ID||''','''||EQUIP_TYPE_CODE||''','''||EQUIP_CLASS_CODE||''','''||CLASS_OF_SERVICE_CODE||''','''||FIXED_CHARGE_AMT||''','''||ADD_FIXED_AMT||''','''||ADD_UNIT_RATE||''','''||ACTIVE_DT||''','''||INACTIVE_DT||''','''||CREATE_DT||''','''||CHANGE_DT||''','''||CHANGE_WHO||''','''||INCREMENTAL_BANDS||''','''||RATE_UNITS_TYPE||''','''||DISTANCE_BAND_ID||''','''||ZONE_CLASS||''','''||ADD_IMPLIED_DECIMAL||''','''||IS_DEFAULT_RATE||''','''||COMPONENT_ID||''','''||BILLING_UNITS_TYPE||''','''||MIN_CHARGE_AMT||''','''||ADD_MIN_AMT || ''') ;' statement from CW_PC_RU where batch_id=$BATCH_ID;
select 'INSERT INTO "RATE_USAGE_BANDS" ("SEQNUM", "RATEBAND", "UNIT_RATE", "NUM_UNITS") VALUES ('''||SEQNUM||''','''||RATEBAND||''','''||UNIT_RATE||''','''||NUM_UNITS||''') ;' statement from CW_PC_RUB where batch_id=$BATCH_ID ORDER BY RATEBAND ASC;
spool CW_NEW_FUP.sql
SELECT 'UPDATE CW_VGE_FUP_REF_DATA SET TOLERANCE='''||TOLERANCE||''',PRICE_DELTA='''||PRICE_DELTA||''',FUP_LOWER='''||FUP_LOWER||''',ALLOWANCE='''||ALLOWANCE||''' WHERE UNIT_CR_ID='||UNIT_CR_ID|| ';' STATEMENT FROM CW_PC_FUP where batch_id=$BATCH_ID;
spool off


quit
THEEND
echo "SQL files are created for INSTALL" >>$log_file

echo "@CW_NEW_MRC.sql" >>sql_list.sql
echo "@CW_NEW_DISCOUNT.sql" >>sql_list.sql
echo "@CW_NEW_UNIT_CREDIT.sql" >>sql_list.sql
echo "@CW_NEW_TARIFF_RATE.sql" >>sql_list.sql
echo "@CW_NEW_USAGE_RATES.sql" >>sql_list.sql
echo "@CW_NEW_FUP.sql" >>sql_list.sql

cd $ROLLBACK_DIR/SQL/

echo 'Going to Connect to database'
touch display.log
sqlplus $ARBOR_USR/`cat $ARBORDIR/.arborpw`@$ORACLE_SID << THEEND >>display.log
clear buffer;
set define off;
set linesize 3000;
set head off;
set feed off
set feedback off;
COLUMN HEADING OFF
column HEADER truncated
column FOOTER truncated

spool CW_OLD_MRC.sql;
SELECT 'DELETE FROM RATE_RC WHERE INACTIVE_DATE=NULL AND ELEMENT_ID=' ||ELEMENT_ID||' AND COMPONENT_ID='||COMPONENT_ID|| '; ' STATEMENT FROM CW_PC_RR where batch_id=$BATCH_ID;
SELECT 'UPDATE RATE_RC SET INACTIVE_DATE=NULL WHERE ELEMENT_ID='||MEMBER_ID||' AND COMPONENT_ID= '||COMPONENT_ID||';' STATEMENT FROM CW_OLD_MRC_RATES where batch_id=$BATCH_ID;

spool CW_OLD_DISCOUNT.sql;
SELECT 'DELETE FROM RATE_DISCOUNT WHERE INACTIVE_DATE=NULL AND DISCOUNT_ID=' ||DISCOUNT_ID|| '; ' STATEMENT FROM CW_PC_RD where batch_id=$BATCH_ID;
SELECT 'UPDATE RATE_DISCOUNT RDIS SET RDIS.DATE_INACTIVE= null WHERE DISCOUNT_ID='||CR.DISCOUNT_ID|| ';' STATEMENT FROM CW_CPF_MODIFY_DISCOUNT_RATES CR where batch_id=$BATCH_ID;

spool CW_OLD_UNIT_CREDIT.sql;
SELECT 'DELETE FROM RATE_UNIT_CR WHERE INACTIVE_DATE=NULL AND UNIT_CR_ID=' ||UNIT_CR_ID|| '; ' STATEMENT FROM CW_PC_RUC where batch_id=$BATCH_ID;
SELECT 'UPDATE RATE_UNIT_CR RDIS SET RDIS.DATE_INACTIVE= null WHERE UNIT_CR_ID='||CR.DISCOUNT_ID|| ';' STATEMENT FROM CW_CPF_MODIFY_UNITCREDIT_RATES CR where batch_id=$BATCH_ID;

spool CW_OLD_TARIFF_RATE.sql;
SELECT 'DELETE FROM TARIFF_RATE WHERE DATE_INACTIVE= NULL AND TARIFF_ID='||CR.TARIFF_ID||' AND BUNDLE_ID='||CR.BUNDLE_ID||'; ' STATEMENT FROM CW_PC_TR CR where batch_id=$BATCH_ID;
SELECT 'UPDATE TARIFF_RATE TR SET TR.DATE_INACTIVE= NULL WHERE TARIFF_ID='||CR.TARIFF_ID||' AND BUNDLE_ID='||CR.BUNDLE_ID||' ;' STATEMENT FROM CW_OLD_TARIFF_SELF_OPT_RATES CR where batch_id=$BATCH_ID;
select 'UPDATE TARIFF_RATE_BAND TRB SET TRB.STEP='||decode(STEP,'','NULL',STEP)||',TRB.RANGE_ORIGIN='||decode(RANGE_ORIGIN,'','NULL',RANGE_ORIGIN)||',TRB.RANGE_TERMINUS='||decode(RANGE_TERMINUS,'','NULL',RANGE_TERMINUS)||' WHERE TRB.TARIFF_ID='||TARIFF_ID||' AND TRB.AGGREGATION_KEY='||AGGREGATION_KEY||' AND TRB.BUNDLE_ID='||BUNDLE_ID||';' statement from CW_OLD_TARIFF_SELF_OPT_RATES where batch_id=$BATCH_ID;

spool CW_OLD_USAGE_RATES.sql;
SELECT 'DELETE FROM RATE_USAGE WHERE SEQNUM = '||RU.SEQNUM|| ';' STATEMENT FROM CW_PC_RU RU where batch_id=$BATCH_ID;
SELECT 'DELETE FROM RATE_USAGE_BANDS WHERE SEQNUM = '||RUB.SEQNUM|| ';' STATEMENT FROM CW_PC_RUB RUB where batch_id=$BATCH_ID;
SELECT 'UPDATE RATE_USAGE RU SET RU.INACTIVE_DT= NULL WHERE SEQNUM='||SEQNUM||';' STATEMENT FROM CW_OLD_USAGE_RATES where batch_id=$BATCH_ID;


spool off

UPDATE CW_PC_BATCH_STATUS SET STATUS = 'PATCH_CREATED' WHERE BATCH_ID=$BATCH_ID;

quit


THEEND
echo "SQL files are created for ROLLBACK" >>$log_file

echo "@CW_OLD_MRC.sql" >>sql_list.sql
echo "@CW_OLD_DISCOUNT.sql" >>sql_list.sql
echo "@CW_OLD_UNIT_CREDIT.sql" >>sql_list.sql
echo "@CW_OLD_TARIFF_RATE.sql" >>sql_list.sql
echo "@CW_OLD_USAGE_RATES.sql" >>sql_list.sql


echo "Patch installed Successfully..">>$log_file
tar -cf $PATCH_DIR/$PATCH_NAME.tar $RELEASE_DIR
cp $PATCH_DIR/$PATCH_NAME.tar $ARBORDIR/cw/delivery

对我来说有些奇怪。请帮忙。

最佳答案

该脚本具有不定向到日志的 echo 语句,但您的 Java 程序没有消耗脚本进程的输出,因此一旦输出缓冲区已满,脚本就会停止。

如果您希望脚本输出到控制台,即与 Java 程序输出相同的输出目的地,您需要使用 ProcessBuilder.inheritIO() .

无论如何,您应该使用ProcessBuilder。引用Process的javadoc :

By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

Where desired, subprocess I/O can also be redirected using methods of the ProcessBuilder class.

As of 1.5, ProcessBuilder.start() is the preferred way to create a Process.

关于java - shell脚本没有完全从java执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33983675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com