如何在shell脚本

时间:2017-06-06 01:13:54

标签: python linux bash shell

我有一个名为python的{​​{1}}脚本。将使用spark.py中的shell脚本调用此scipt。

Linux如下所示:

spark.py

#!/usr/bin/env python import sys import os if len(sys.argv) != 2: print "Invalid number of args......" print "Usage: spark-submit file.py Arguments" exit() table=sys.argv[1] hivedb=sys.argv[2] from pyspark import SparkContext, SparkConf conf = SparkConf() sc = SparkContext(conf=conf) from pyspark.sql import HiveContext sqlContext = HiveContext(sc) from datetime import datetime df.registerTempTable('mytempTable') date=datetime.now().strftime('%Y-%m-%d %H:%M:%S') try: sqlContext.sql("create table {}.`{}` as select * from mytempTable".format(hivedb,table)) except Exception as e: status = 'fail' error_message = e else: # Executes only if no Exception. status = 'success' error_message = 'No error' print error_message print ("{},{},{},{},{}".format(hivedb,table,date,status,error_message)) if status != 'success': sys.exit(1) sc.stop() 如下所示

shell.sh

在这个#!/bin/bash source /home/$USER/source.sh [ $# -ne 2 ] && { echo "Usage : $0 input file "; exit 1; } table=$1 hivedb=$2 TIMESTAMP=`date "+%Y-%m-%d"` touch /home/$USER/logs/${TIMESTAMP}.success_log touch /home/$USER/logs/${TIMESTAMP}.fail_log success_logs=/home/$USER/logs/${TIMESTAMP}.success_log failed_logs=/home/$USER/logs/${TIMESTAMP}.fail_log #Function to get the status of the job creation function log_status { status=$1 message=$2 if [ "$status" -ne 0 ]; then echo "$result" | tee -a "${failed_logs}" else echo "$result" | tee -a "${success_logs}" fi } result=$(spark-submit --name "Spark" --master "yarn-client" /home/$USER/spark.py ${table} ${hivedb}) g_STATUS=$? log_status $g_STATUS "$result" 脚本中,我将shell的输出作为变量收集。当我这样做时,我无法在spark.py的控制台日志中看到print的任何spark.py命令。

如何在Linux中打印所有print命令。

在我的linux console logs脚本中我有

spark.py

如何在print error_message print ("{},{},{},{},{}".format(hivedb,table,date,status,error_message)) 中将输出收集为变量时排除print error_message

2 个答案:

答案 0 :(得分:0)

我能想到的最简洁的方法是创建一个没有print error_messsage的新文件,并在shell.sh中使用它...

$ sed '/print\serror_message/d' "/home/${USER}/spark.py" > "/home/${USER}/spark_no_err_msg.py"

答案 1 :(得分:0)

一种简单的方法是将echo "$result"添加到shell脚本中。您还可以修改子命令以在末尾添加teeresult=$(... | tee /dev/stderr )