将成员/变量从python脚本传递给shell脚本或命令

时间:2016-01-18 10:52:27

标签: python linux bash shell

我正在尝试使用python脚本从xml文件中获取的变量。一旦获得变量,我想将这些变量传递给由shell脚本运行的命令。需要帮助

我的Python脚本类似于下面的

#!/usr/bin/python

import os
import xml.etree.ElementTree as etree
from hive import ThriftHive
filename = "mood_ib_history_parameters_DEV.xml"
__currentlocation__ = os.getcwd()
__fullpath__ = os.path.join(__currentlocation__,filename)

tree = etree.parse(__fullpath__)
root = tree.getroot()

hive_db = root.find("hive_db").text
EDGE_HIVE_CONN = root.find("EDGE_HIVE_CONN").text
target_dir = root.find("target_dir").text
to_email_alias = root.find("to_email_alias").text
to_email_cc = root.find("to_email_cc").text
from_email_alias = root.find("from_email_alias").text
dburl = root.find("dburl").text
SQOOP_EDGE_CONN = root.find("SQOOP_EDGE_CONN").text
user_name = root.find("user_name").text
password = root.find("password").text
IB_log_table = root.find("IB_log_table").text
SR_DG_master_table = root.find("SR_DG_master_table").text
SR_DG_table = root.find("SR_DG_table").text

print hive_db
print EDGE_HIVE_CONN
print target_dir
print to_email_alias
print to_email_cc
print from_email_alias
print dburl
print SQOOP_EDGE_CONN
print user_name
print password
print IB_log_table
print SR_DG_master_table
print SR_DG_table

变量将是

  • hive_db
  • EDGE_HIVE_CONN
  • TARGET_DIR
  • to_email_alias
  • to_email_cc
  • from_email_alias
  • dburl
  • SQOOP_EDGE_CONN
  • USER_NAME
  • 密码
  • IB_log_table
  • SR_DG_master_table
  • SR_DG_table

将在shell脚本中运行的shell命令如下所示

ssh -i /apps/phodisvc/.ssh/id_rsa_edge_node ${SQOOP_EDGE_CONN} "sqoop import -D mapred.child.java.opts='\-Djava.security.egd=file:/dev/../dev/urandom' --connect '${dburl}' --username ${user_name} --password ${password} --query \"select CONTRACT_ID,CONTRACT_NUMBER,CONTRACT_STS_CODE,CONTRACT_STATUS,SERVICE_LINE_ID from XXCCS where \\\$CONDITIONS \" --split-by CONTRACT_NUMBER -m 4 --null-string '\\\\N' --null-non-string '\\\\N' --hive-delims-replacement '<EOL>' --boundary-query 'select (select min(CONTRACT_NUMBER) from XXCCS_DS_SAHDR_CORE) as minid ,(select max(CONTRACT_NUMBER) from XXCCS_DS_SAHDR_CORE) as maxid from dual' --target-dir ${target_dir}/XXCCS_DS_SAHDR_CORE --hive-import --hive-overwrite --hive-table ${hive_db}.XXCCS --map-column-hive CONTRACT_ID=BIGINT,SERVICE_LINE_ID=BIGINT"

我不想在第一个脚本中使用call()或subprocess()方法,因为我想将任务划分为模块。需要帮助。

1 个答案:

答案 0 :(得分:0)

一旦你知道变量,你只需要传递给shell中的命令行。

cmd = 'ssh -i /apps/phodisvc/.ssh/id_rsa_edge_node {0} "sqoop import -D mapred.child.java.opts='\-Djava.security.egd=file:/dev/../dev/urandom' --connect '{1}' --username {2} --password {3} --query \"select CONTRACT_ID,CONTRACT_NUMBER,CONTRACT_STS_CODE,CONTRACT_STATUS,SERVICE_LINE_ID from XXCCS where \\\$CONDITIONS \" --split-by CONTRACT_NUMBER -m 4 --null-string '\\\\N' --null-non-string '\\\\N' --hive-delims-replacement '<EOL>' --boundary-query 'select (select min(CONTRACT_NUMBER) from XXCCS_DS_SAHDR_CORE) as minid ,(select max(CONTRACT_NUMBER) from XXCCS_DS_SAHDR_CORE) as maxid from dual' --target-dir ${target_dir}/XXCCS_DS_SAHDR_CORE --hive-import --hive-overwrite --hive-table ${hive_db}.XXCCS --map-column-hive CONTRACT_ID=BIGINT,SERVICE_LINE_ID=BIGINT"'.format(SQOOP_EDGE_CONN,user_name,password,...)

os.sytem(Cmd)

文件&#39; S

String formmatting operator Ë Execute command in subshell