我需要一些简单脚本的帮助。该脚本的输入是一个包含的同类文件的目录:Bash - 将文件连接到按日期排序的目录中
FILENAME20160220.TXT
FILENAME20160221.TXT
FILENAME20160222.TXT
...
脚本需要有该目录作为输入,它们连接成一个所谓的新文件:
FILENAME.20160220_20160222.TXT
上述文件名需要找到它的“最早”_“最新”日期。迄今为止我写的剧本就是这样,但它并没有产生必要的输出。有人能帮我修补它吗?
declare FILELISTING="FILELISTING.TXT"
declare SOURCEFOLDER="/Cat_test/cat_test/"
declare TEMPFOLDER="/Cat_Test/cat_test/temp/"
# Create temporary folder
cd $SOURCEFOLDER
mkdir $TEMPFOLDER
chk_abnd $?
# Move files into temporary folder
mv *.TXT $SOURCEFOLDER $TEMPFOLDER
chk_abnd $?
# Change directory to temporary folder
cd $TEMPFOLDER
chk_abnd $?
# Iterate through files in temp folder and create temporary listing files
for FILE in $TEMPFOLDER
do
echo $FILE >> $FILELISTING
done
# Iterate through the lines of FILELISTING and store dates into array for sorting
while read lines
do
array[$i] = "${$line:x:y}"
((i++))
done <$FILELISTING
# Sort dates in array
for ((i = 0; i < $n ; i++))
do
for ((j = $i; j < $n; j++))
do
if [ $array[$i] -gt $array[$j] ]
then
t=${array[i]}
array[$i]=${array[$j]}
array[$j]=$t
fi
done
done
# Get first and last date of array and construct output filename
OT_FILE=FILENAME.${array[1]}_${array[-1]}.txt
# Sort files in folder
# Cat files into one
cat *.ACCT > "$OT_FILE.temp"
chk_abnd $?
# Remove Hex 1A
# tr '\x1A' '' < "$OT_FILE.temp" > $OT_FILE
# Cleanup - Remove File Listing
rm $FILE_LISTING
chk_abnd $?
rm $OT_FILE.temp
chk_abnd $?
这将是很好,如果你可以问一个更具体的问题你的脚本的特定部分,如“10号线没有并置像我期望的,我遇到错误信息XYZ ”。有关更多信息,请参阅[mcve]上的帮助页面。 –
您可以通过http://www.shellcheck.net/运行您的代码。当我这样做时,它标记了一个明确的错误和一些较小的警告。 –
在脚本的开头添加'set -x'并找出执行流程并适当地指出失败部分。 – Inian