【发布时间】:2020-10-01 19:45:26
【问题描述】:
我在 ${MY_DIR} 有一个文件列表。
ABC_foo123.txt
ABC_foo456.txt
ABC_bar100.txt
需要对这些文件做一些操作。
我的代码:
#!/usr/bin/ksh
ls -l ${MY_DIR}/ABC_foo*.txt
RV=$?
if [[ $RV -eq 0 ]];then
echo "files found for processing"
for sFile in `ls -1 ${MY_DIR}/ABC_foo*.txt`
do
fileNameOnly=$(basename ${sFile})
echo processing $sFile
#Doing some operations here
done
else
echo "No foo files found to process"
fi
ls -l ${MY_DIR}/ABC_bar*.txt
RV=$?
if [[ $RV -eq 0 ]];then
echo "files found for processing"
for sFile in `ls -1 ${MY_DIR}/ABC_bar*.txt`
do
fileNameOnly=$(basename ${sFile})
echo processing $sFile
#Doing some operations here
done
else
echo "No bar files found to process"
fi
这是按预期进行的。但是,有没有一种方法可以使用一个循环而不是两个循环。我觉得我在做同样的事情两次。 欢迎任何建议
【问题讨论】:
-
我认为
ls -1 ${MY_DIR}/ABC_bar*.txt应该是ls -1 ${MY_DIR}/ABC_bar*.sql。