问题描述
我有一个shell脚本.我要向该脚本传递文件中的参数.该文件包含表名
I have a shell script. To this script I am passing arguments from a file. This file contains tables names
脚本运行正常.我可以对文件中的所有表执行命令.
The script is working fine. I am able execute the command for all the tables in the file.
shell script
#!/bin/bash
[ $# -ne 1 ] && { echo "Usage : $0 input file "; exit 1; }
input_file=$1
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 "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
#echo "Please find the attached log file for more details"
#exit 1
else
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
fi
}
while read table ;do
sqoop job --exec $table > /home/$USER/logging/"${table}_log" 2>&1
done < ${input_file}
g_STATUS=$?
log_status $g_STATUS "Sqoop job ${table}"
我正在尝试收集脚本的status logs
.
I am trying to collect the status logs
for the script.
我想分别收集每个表的status logs
.
I want to collect the status logs
for each table individually.
我想要什么
2017-04-28 20:36:41 [ERROR] sqoop job table1 EXECUTION [Status] 2 : failed
2017-04-28 20:36:41 [ERROR] sqoop job table2 EXECUTION [Status] 2 : failed
我得到的是
如果最后一张表的脚本失败
If the script for last table fails
2017-04-28 20:38:41 [ERROR] sqoop job EXECUTION [Status] 2 : failed
如果最后一个表的脚本成功,则
If the script for the last table is successful then
2017-04-28 20:40:41 [ERROR] sqoop job [Status] 0 : success
我在做错什么,我应该做些什么改变才能获得理想的结果.
What am I doing wrong and what changes should I make to get desired results.
推荐答案
更改
while read table ;do
sqoop job --exec $table > /home/$USER/logging/"${table}_log" 2>&1
done < ${input_file}
g_STATUS=$?
log_status $g_STATUS "Sqoop job ${table}"
到
while read table ;do
sqoop job --exec $table > /home/$USER/logging/"${table}_log" 2>&1
g_STATUS=$?
log_status $g_STATUS "Sqoop job ${table}"
# Any other command you want to run on using $table should be placed here
done < ${input_file}
while
循环仅在while
和done
行中运行代码.因此,要记录所有表,您需要在while循环中运行记录.
The while
loop only runs the code within the while
and done
lines. So to log for all tables, you need to run the logging inside the while loop.
此外,$table
会在循环迭代中更改,因此要在所有表上运行的任何命令都需要在循环内运行.
Also, $table
changes in iteration of the loop, so any command that you want to run on all tables, you need to run inside the loop.
这篇关于如何在Linux/bash中为Shell脚本创建状态日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!