LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   execute script shell with regex as argument (https://www.linuxquestions.org/questions/linux-newbie-8/execute-script-shell-with-regex-as-argument-775805/)

eguider 12-15-2009 10:08 AM

execute script shell with regex as argument
 
hi all

I want to create a script shell that accept a regular expression as argument.

exemple :

./my_script.sh "[a-zA-Z0-9]*\.doc"

my script contains :

#!/bin/bash
find ./ -regextype posix-extended -regex "\./$2" -type f"


but the problem is that (* and {x,y}...) in regular expression is interpreted diferently :s

can you help me plz.

catkin 12-15-2009 10:16 AM

Four ideas.

Firstly bash interprets \ characters in double-quoted strings so better change "[a-zA-Z0-9]*\.doc" to '[a-zA-Z0-9]*\.doc' on the command line so the script receives the string verbatim.

Secondly "[a-zA-Z0-9]*\.doc" is the only command line argument passed to the script so it is $1, not $2 as used in the find command.

Thirdly the same problem as the first in "\./$2". Solution to use "\\./$2" or '\./'"$2".

Finally there is an unbalanced trailing double quote in find ./ -regextype posix-extended -regex "\./$2" -type f"

ghostdog74 12-15-2009 07:26 PM

any reason why you want to use regex with find?
Code:


for file in [a-zA-Z0-9]*.doc
do
 echo "found: $file"
done



All times are GMT -5. The time now is 06:14 AM.