S
S
Saboteur2015-07-29 13:18:15
linux
Saboteur, 2015-07-29 13:18:15

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}

But there was a problem that the path from the root is immediately loaded into $ITEM, and executing no longer displays a mask, but a bunch of file names.
I understand that file masks without quotes are immediately opened, but I can't figure out how to arrange them.
Who can immediately suggest a better option?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2015-07-29
@saboteur_kiev

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

M
Max, 2015-07-29
@MaxDukov

the variant with find will not approach? those. find and remove. However, I'm not sure that it is possible to limit the nesting level (i.e. do not delete from the first and second level directories, delete from the third)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question