samedi 25 avril 2015

Building the find regex parameters in shell scripts


I'm trying to build the parameters used in the regex expression in a find command from a shell script but it seems does not work.

Objective of this shell script is to be able to find some files according with specified parameters in shell script.

The shell script looks like something like:

#!/bin/bash
IDS=$1
FOLDER="/tmp"
MODULENAMEPATTERN=`echo $IDS | sed "s/,/|/g"`
MODULENAMEPATTERN=".*\\.\\($MODULENAMEPATTERN\\)"
echo "find command: find $FOLDER -follow -type f -regex \"$MODULENAMEPATTERN.suffix\""

for FILEFOUND in `find $FOLDER -follow -type f -regex "$MODULENAMEPATTERN.suffix"`; do
    echo $FILEFOUND
done;

To launch it, I use the following command:

./test pattern1,pattern2

it generate the following output:

find command: /tmp -follow -type f -regex ".*\.\(pattern1\|pattern2\).suffix"

But nothing more.

Unfortunately, if I execute the generated find command from a terminal, it generate the following output:

/tmp/folder1/.pattern1.suffix
/tmp/folder2/.pattern1.suffix
/tmp/folder2/.pattern2.suffix

I do know exactly where is my problem. Can you help me?

Regards


Aucun commentaire:

Enregistrer un commentaire