![Bash - 无法统计在 for...do 循环中创建的最后一个文件](https://linux22.com/image/225184/Bash%20-%20%E6%97%A0%E6%B3%95%E7%BB%9F%E8%AE%A1%E5%9C%A8%20for...do%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E5%88%9B%E5%BB%BA%E7%9A%84%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6.png)
在 bash 脚本中(在 Ubuntu 22 上,如果重要的话)我有一个函数,从 for...do 循环中执行,并给出一个文件名来执行一些 SQLplus <query_name>.sql 并将结果存储在 <query_name> 中.csv
我添加了一些函数来检查是否成功并打印出刚刚创建的 csv 文件大小。
但是...它会运行三个查询并在成功消息中报告文件大小,没有问题。但对于第四个也是最后一个,它会抛出一个错误,指出“无法 statx:没有这样的文件或目录”。我可以看到它就在那里。作为替代方案,我让整个过程列出目录中的文件,然后它就会出现。
知道为什么它可能不会以同样的方式做最后一个。
这是 run_query() 和 success_file() 函数的代码,我认为您只需要看看我在做什么:
[编辑以显示整个脚本文件而不仅仅是片段]
#!/bin/bash
source ~/overnights/scripts/.globals/.filenames
source ~/overnights/scripts/.globals/.creds
source ~/overnights/scripts/.globals/.commands.sh
echo "============================================================================="
echo "= Starting to fetch Banner data ="
echo "============================================================================="
# cleandrop() called from each sql script execution to finish the whole process if one file fails.
cleandrop () {
echo "`date`: Problems connecting to or fetching from Oracle. Cleaning up and exiting."
rm -f ${IN_BANNER}/*
cd $SCRIPT_DIR
exit 1
}
# Displays confirmations of which file we have just processed.
success_file () {
size=$(stat -c%s "$2")
echo "`date`: SQL script executed successfully for $1"
echo "`date`: Output file saved to $2 (${size} bytes)."
echo
}
# Given a query name from the array "queries" performs the query and saves the output.
run_query () {
SQL="${BANNER_SCRIPTS}/$1.sql"
CSV="${IN_BANNER}/$1.csv"
echo "`date`: Beginning ${SQL} processing."
sqlplus64 -S "${OUSER}/${OPASS}@//${OHOST}:${OPORT}/${OSID}" @${SQL}
# Check the exit status of sqlplus to see if the SQL script ran successfully
if [[ $? -eq 0 ]]
then
success_file ${SQL} ${CSV}
else
cleandrop
fi
}
# Try a connection just to make sure it works first otherwise exit
echo "exit" | sqlplus64 -L "${OUSER}/${OPASS}@//${OHOST}:${OPORT}/${OSID}" | grep Connected > /dev/null
if [[ $? -ne 0 ]]
then
cleandrop
else
echo "`date`: Connection checked okay, proceeding to fetch Banner data."
fi
# Remove files from previous runs if they exist.
cd $IN_BANNER
signal="${IN_BANNER}/DONE.txt"
if [[ -f "$signal" ]]
then
echo "`date`: Removing signal file ${signal}"
rm -f $signal
echo "`date`: Removing any previous data files in ${IN_BANNER}"
echo
rm -f ${IN_BANNER}/*.csv
fi
rm -f *
# Set up the array of query names to execute with run_query()
queries=( ora_SRS_PEOPLE
ora_SRS_COURSES
ora_SRS_ENROLS
ora_SRS_STAFF
)
for query in "${queries[@]}"
do
run_query ${query}
done
# drop in a signal file so other processes can tell if the downloads have completed.
echo "`date`: Creating completion signal file ${signal}"
touch ${signal}
echo
echo "`date`: Listing of data files collected in ${IN_BANNER}"
ls -lh ${IN_BANNER}
cd ${SCRIPT_DIR}
echo
echo "`date`: overnights_oracle.sh completed"
exit 0
...这是我看到的输出:
=============================================================================
= Starting to fetch Banner data =
=============================================================================
Sat Oct 7 15:55:53 BST 2023: Connection checked okay, proceeding to fetch Banner data.
Sat Oct 7 15:55:53 BST 2023: Removing signal file /home/p0071665/overnights/data/downloads/banner/DONE.txt
Sat Oct 7 15:55:53 BST 2023: Removing any previous data files in /home/p0071665/overnights/data/downloads/banner
Sat Oct 7 15:55:53 BST 2023: Beginning /home/p0071665/overnights/scripts/sql/banner/ora_SRS_PEOPLE.sql processing.
Sat Oct 7 15:57:54 BST 2023: SQL script executed successfully for /home/p0071665/overnights/scripts/sql/banner/ora_SRS_PEOPLE.sql
Sat Oct 7 15:57:54 BST 2023: Output files saved to /home/p0071665/overnights/data/downloads/banner/ora_SRS_PEOPLE.csv (10776253 bytes).
Sat Oct 7 15:57:54 BST 2023: Beginning /home/p0071665/overnights/scripts/sql/banner/ora_SRS_COURSES.sql processing.
Sat Oct 7 15:58:04 BST 2023: SQL script executed successfully for /home/p0071665/overnights/scripts/sql/banner/ora_SRS_COURSES.sql
Sat Oct 7 15:58:04 BST 2023: Output files saved to /home/p0071665/overnights/data/downloads/banner/ora_SRS_COURSES.csv (3992287 bytes).
Sat Oct 7 15:58:04 BST 2023: Beginning /home/p0071665/overnights/scripts/sql/banner/ora_SRS_ENROLS.sql processing.
Sat Oct 7 16:01:07 BST 2023: SQL script executed successfully for /home/p0071665/overnights/scripts/sql/banner/ora_SRS_ENROLS.sql
Sat Oct 7 16:01:07 BST 2023: Output files saved to /home/p0071665/overnights/data/downloads/banner/ora_SRS_ENROLS.csv (26717546 bytes).
Sat Oct 7 16:01:07 BST 2023: Beginning /home/p0071665/overnights/scripts/sql/banner/ora_SRS_STAFF.sql processing.
Sat Oct 7 16:01:11 BST 2023: SQL script executed successfully for /home/p0071665/overnights/scripts/sql/banner/ora_SRS_STAFF.sql
Sat Oct 7 16:01:11 BST 2023: Output files saved to /home/p0071665/overnights/data/downloads/banner/ora_SRS_STAFF.csv ( bytes).
Sat Oct 7 16:01:11 BST 2023: Creating completion signal file /home/p0071665/overnights/data/downloads/banner/DONE.txt
Sat Oct 7 16:01:11 BST 2023: Listing of data files collected in /home/p0071665/overnights/data/downloads/banner
total 41M
-rw-r--r-- 1 p0071665 p0071665 0 Oct 7 16:01 DONE.txt
-rw-r--r-- 1 p0071665 p0071665 3.9M Oct 7 15:58 ora_SRS_COURSES.csv
-rw-r--r-- 1 p0071665 p0071665 26M Oct 7 16:01 ora_SRS_ENROLS.csv
-rw-r--r-- 1 p0071665 p0071665 11M Oct 7 15:57 ora_SRS_PEOPLE.csv
-rw-r--r-- 1 p0071665 p0071665 1.2M Oct 7 16:01 ora_SRS_STAFF_ENROLS.csv
这是我运行它时在控制台中得到的内容:
stat: cannot statx '/home/p0071665/overnights/data/downloads/banner/ora_SRS_STAFF.csv': No such file or directory
但我知道文件就在那里。我可以移动和复制它,它在 ls -al 等中列出。
我想知道我是否必须做一些事情来“关闭”文件,然后 stat 才能找到它,但我在执行 stat 之前添加了“触摸”和“删除”信号文件,但没有效果。这并不完全重要,但它让我烦恼为什么它不能仅对要运行的查询数组中的一个元素起作用。
干杯,乔克
答案1
这看起来可能是您的代码中的拼写错误。没有ora_SRS_STAFF.csv
,所以错误消息是正确的:
-rw-r--r-- 1 p0071665 p0071665 0 Oct 7 16:01 DONE.txt -rw-r--r-- 1 p0071665 p0071665 3.9M Oct 7 15:58 ora_SRS_COURSES.csv -rw-r--r-- 1 p0071665 p0071665 26M Oct 7 16:01 ora_SRS_ENROLS.csv -rw-r--r-- 1 p0071665 p0071665 11M Oct 7 15:57 ora_SRS_PEOPLE.csv -rw-r--r-- 1 p0071665 p0071665 1.2M Oct 7 16:01 ora_SRS_STAFF_ENROLS.csv