1

Here is my code:

if [ -f /Applications/xml[0-9].pl ]

I also tried with -f =~ and double brackets but none seem to work.

I first though that the regex might be faulty but I tried this.

echo h1 | sed 's/h[0-9]/h/'

And that worked. What am I doing wrong here?

DisplayName
  • 11,688

2 Answers2

3

You can't test a file with a regex nor a glob like this. You have to iterate over the files :

for file in /Applications/xml[0-9].pl; do if [ -f "$file" ]; then ...
1

bash expands the regular expression before evaluating the condition. If your directory has three files by the name

/Applications/xml0.pl
/Applications/xml1.pl
/Applications/xml2.pl

your if statement looks to shell as

if [ -f /Applications/xml0.pl /Applications/xml1.pl /Applications/xml2.pl ]]

which is syntactically incorrect. You will need to specify the filename completely when you want to look for its existence by using

for file in /Applications/xml[0-9].pl
do
    if [ -f ${file} ]

or if you can, use the find command to do whatever you want done with the file.

unxnut
  • 6,008