Can this bash function be optimized?
Greetings,
I have a bash script that parses ZIP files we receive from a client and uncompresses them if a set of criterias is matched. It works well but it is slow. Particularly, the following function:
function getCTLfile() {
for i in ${HDD_LIST_Array[@]}
do
if [[ `echo ${i}|awk -F . '{print $NF}'` == "ctl" ]]
then
echo "${i}"
fi
done
}
This function's purpose is to get name of the control file contained in a ZIP file. HDD_LIST_Array[@]
is obtained thusly for 开发者_JAVA百科each zip file:
HDD_LIST_Array=(`unzip -l $name | head -n -2|tail -n +4 | sort -r | awk '{print $4}'`)
Again, it works, albeit slowly. Can this function be optimized to run faster? Any advice?
Thanks.
unzip -l
takes a file pattern to match after the input filename, and returns 0 on finding it or 11 on failure.
entry=$(unzip -l -qq "$name" '*.ctl')
if [ $? -eq 0 ]
then
awk '{ print $4 }' <<< "$entry"
done
You could use cut
instead of awk
and &&
instead of if
but that is probably minor if anything. I would assume your largest wall-clock is the IO on the unzip, yes?
Put a time
command around the whole call as well as prefixed to unzip
to get your % differences.
OTHERWISE: Looks like you want to just search and print for all files that end in *.ctl
or something like that within a line-window (head|tail)? Can you rather just grep
or sed
from your unzip output? I'm betting an unzip -l |awk
script will be sufficient. I'll update answer as you provide more detail.
Just a little comment to complement mobrule's reply.
The '-' appears in unwanted lines. You need two characters to differentiate, maybe a ':', or egrep '-[0-9]'
精彩评论