【发布时间】:2016-01-18 10:52:27
【问题描述】:
我正在尝试使用通过 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
- 用户名
- 密码
- 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() 方法,因为我想将任务划分为模块。需要帮助。
【问题讨论】:
-
无论您使用何种方式执行 shell 命令,所有变量都必须在此时对代码可用。因此,即使您将任务划分为子程序,您也可以从中返回变量。但是,如果变量在代码中的某个时刻并非全部已知,则无法使用这些变量调用 shell 函数。 (你的代码不能告诉 shell 代码本身不知道什么)
-
嗨@VBB变量总是来自第一个脚本,第二个脚本只有在第二个脚本成功执行后才会执行。