Bash 如何在shell脚本中延迟命令替换?
我有一个bash脚本,简而言之,它通过其他命令生成一个文件(工作正常),然后在脚本的后面,我对生成的文件执行wc命令。我的问题是,我在wc命令上使用命令替换,当我执行脚本时,它会立即执行此替换,而不是等待在脚本中较早生成文件。我有什么选择 该脚本是一个shell程序,用于运行Oracle SQLLoader将数据加载到Oracle表中。SQLLoader命令生成一个包含被拒绝记录的文件,我正在尝试对其进行字数统计。这是我的代码:Bash 如何在shell脚本中延迟命令替换?,bash,oracle,shell,sh,sql-loader,Bash,Oracle,Shell,Sh,Sql Loader,我有一个bash脚本,简而言之,它通过其他命令生成一个文件(工作正常),然后在脚本的后面,我对生成的文件执行wc命令。我的问题是,我在wc命令上使用命令替换,当我执行脚本时,它会立即执行此替换,而不是等待在脚本中较早生成文件。我有什么选择 该脚本是一个shell程序,用于运行Oracle SQLLoader将数据加载到Oracle表中。SQLLoader命令生成一个包含被拒绝记录的文件,我正在尝试对其进行字数统计。这是我的代码: #!/bin/sh # Set variables pro
#!/bin/sh
# Set variables
program_name="My Program Name"
input_dir="/interface/inbound/hr_datafile"
log_dir="/interface/inbound/log"
bad_dir="/interface/inbound/hr_bad"
archive_dir="/interface/inbound/hr_archive"
input_base="Import_20171213"
input_ext="csv"
control_file="data_loader.ctl"
exit_status=0
d=`date "+%Y%m%d"`
t=`date "+%H%M"`
# Check if data file exists, count records
if [ -f ${input_dir}/${input_base}*.${input_ext} ]; then
data_file_name=`ls -1rt ${input_dir}/${input_base}*.${input_ext} | head -1 | cut -c 32-100`
data_file_base=${data_file_name%.*}
echo "Data file name: " ${data_file_name}
echo "Data file base: " ${data_file_base}
no_of_recs=`expr $(wc -l ${input_dir}/${data_file_name} | cut -c 1-8) - 1`
echo "DEBUG no_of_recs: " ${no_of_recs}
no_of_errs=0
else
echo
echo
echo
echo "----------------------------- ${program_name} ------------------------------------------"
echo "----------------------------------- Error report : ------------------------------------------------"
echo
echo "Please place your Data files in the UNIX directory => "${input_dir}
echo "${program_name} Process exiting ..."
echo
echo "---------------------------------------------------------------------------------------------------"
echo
echo
echo
echo
echo
exit 1
fi
# Run SQL*Loader
echo
echo "==> Loading Data...into MY_TABLE table"
echo
# Create a temporary control file to pass the data file name in
cat $XX_TOP/bin/${control_file} | sed "s/:FILENAME/${data_file_name}/g" > $XX_TOP/bin/${data_file_base}_temp.ctl
# NOTE: The following sqlldr format is used to "hide" the oracle user name and password
sqlldr errors=100000 skip=1 control=$XX_TOP/bin/${data_file_base}_temp.ctl data=${input_dir}/${data_file_name} log=${log_dir}/${data_file_base}.log bad=${bad_dir}/${data_file_base}.bad <<-END_SQLLDR > /dev/null
apps/`cat ${DB_SCRIPTS}/.${ORACLE_SID}apps`
END_SQLLDR
exit_status=$?
echo "DEBUG exit_status " ${exit_status}
# Remove temporary control file
rm -rf $XX_TOP/bin/${data_file_base}_temp.ctl
# Check for Errors
if [ -f ${bad_dir}/${data_file_base}.bad ]; then
echo
echo "----------------------------- ${program_name} ------------------------------------------"
echo "----------------------------------- Error report : ------------------------------------------------"
echo
grep 'Record' ${log_dir}/${data_file_base}.log > ${log_dir}/${data_file_base}.rec
grep 'ORA' ${log_dir}/${data_file_base}.log > ${log_dir}/${data_file_base}.err
paste ${log_dir}/${data_file_base}.rec ${log_dir}/${data_file_base}.err ${bad_dir}/${data_file_base}.bad
echo
echo "<---------------------------------End of Error Report---------------------------------------------->"
echo
# Count error records
no_of_errs=$(wc -l ${bad_dir}/${data_file_base}.bad | cut -c 1-8)
no_of_recs=$(expr ${no_of_recs} - ${no_of_errs})
# Remove temp files
rm ${log_dir}/${data_file_base}.rec
rm ${log_dir}/${data_file_base}.err
rm ${bad_dir}/${data_file_base}.bad
else
echo "Bad File not found at ${bad_dir}/${data_file_base}.bad"
fi
if (( ${no_of_errs} > 0 )); then
echo "Error found in data file..."
exit_status=1
else
# Archive the data file if no errors
mv ${input_dir}/${data_file_name} ${archive_dir}/${data_file_base}_$d"_"$t.${input_ext}
echo "Data file archived to ${archive_dir}"
exit_status=0
fi
echo
echo
echo
echo "----------------------------- ${program_name} ------------------------------------------"
echo
echo "Total records errored out :" ${no_of_errs}
echo "Total records successfully read :" ${no_of_recs}
echo "---------------------------------------------------------------------------------------------------"
echo
# Final Exit Status
if [ ${exit_status} -eq 1 ]; then
echo "==> Exiting process...Status : ${exit_status}"
exit 1
fi
在代码中,.bad文件上的wc命令在sqlldr部分之后执行,但是日志显示了调用sqlldr之前发生的错误
任何想法都将不胜感激!谢谢 您没有看到预期的所有输出-调用SQL*Loader之后的任何内容都丢失了-并且
wc
中的错误出现在错误的位置,在用户名之前而不是之后代码>提示。但是给出您使用的构造不应该是错误的-如果文件不存在,if
测试应该停止该行的访问
问题是,它并没有看到heredoc的终结。heredoc开始之外的所有命令都是作为heredoc处理的一部分执行的,但不会执行到任何地方或显示到终端(如预期的那样),并且所有被评估的方式中的某些东西会导致wc
意外运行,即使该文件不存在。您看到的是来自它的stderr
输出
这一切都发生在SQL*Loader启动之前,因此您会在启动之后看到用户名:
提示
从评论中可以看出,herdeoc ENDEND\u SQLLDR
在您的实际代码中是缩进的,这并没有反映在发布的问题中。当您使用时,是否可以尝试在第一行之后使用&&可能重复的旁注:您可以使用wc-l
仅获取没有文件名的数字。为什么要使用eval
?这甚至不是有效的语法。你能详细解释一下为什么说“这在我的脚本输出中出现之前就应该出现”吗?您希望先运行哪个命令?为什么?您确定您的OS/shell版本理解
Data file name: Import_20171213.csv
Data file base: Import_20171213
DEBUG no_of_recs: 27
==> Loading Data...into MY_TABLE table
wc: 0653-755 Cannot open /interface/inbound/hr_bad/Import_20171213.bad.
Username:
SQL*Loader: Release 10.1.0.5.0 - Production on Thu Dec 21 12:42:39 2017
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Commit point reached - logical record count 27
Program exited with status 2