Answer the question
In order to leave comments, you need to log in
Bash How to use wildcards in variables so they don't get expanded ahead of time?
The task is something like this:
I want to delete certain files in subdirectories. I find subdirectories through find, then in the desired directory you need to delete files by mask.
At first I usually did
... a lot of script, as a result there is the necessary directory in ${CURRENTDIR}
rm -rf ${CURRENTDIR}/*/*/*.tgz
rm -rf ${CURRENTDIR}/*/*/*.vmdk. zip
Then I decided to put the list of masks for deleting files into a loadable variable, where the masks are separated:
#!/bin/bash
DELETEITEMS="/*/*/*.tar.gz:/*/*/*.vmdk.zip:/*/*/*.ext3:/ipk/*/*.ipk"
OIFS=${IFS}
IFS=":"
for ITEM in ${DELETEITEMS}
do
echo "executing:"."${ITEM}"
done
IFS=${OIFS}
Answer the question
In order to leave comments, you need to log in
I figured it out myself. It is necessary to use quotes and arrays.
In the example below, typeset -a is optional, but it is required if the DELETEITEMS assignment comes from another variable (for example, I first read LINE from some file, and then I assign typeset -a DELETEITEMS="$LINE". But the main thing is that the masks files will not be expanded until you use them without quotes, i.e. in echo "executing: ${ITEM}" it is still a mask, and in rm -rf $ITEM it is already a list of files by mask
#!/bin/ bash
typeset -a DELETEITEMS=("/*/*/*.epk" "/*/*/*.tar.gz" "/*/*/*.vmdk.zip" "/*/*/*.ext3 " "/ipk/*/*.ipk")
for ITEM in "${DELETEITEMS[@]}"
do
echo "executing: ${ITEM}"
rm ${ITEM}
done
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question