thiesdiggity
thiesdiggity

Reputation: 1967

String contains in Bash that is a directory path

I am writing an SVN script that will export only changed files. In doing so I only want to export the files if they don't contain a specific file.

So, to start out I am modifying the script found here.

I found a way to check if a string contains using the functionality found here.

Now, when I try to run the following:

filename=`echo "$line" |sed "s|$repository||g"`
if [ ! -d $target_directory$filename ] && [[!"$filename" =~ *myfile* ]] ; then

fi

However I keep getting errors stating:

/home/home/myfile: "no such file or directory"

It appears that BASH is treating $filename as a literal. How do I get it so that it reads it as a string and not a path?

Thanks for your help!

Upvotes: 0

Views: 3469

Answers (1)

that other guy
that other guy

Reputation: 123460

You have some syntax issues (a shell script linter can weed those out):

  • You need a space after "[[", otherwise it'll be interpretted as a command (giving an error similar to what you posted).
  • You need a space after the "!", otherwise it'll be considered part of the operand.

You also need something in the then clause, but since you managed to run it, I'll assume you just left it out.

  • You combined two difference answers from the substring thing you posted, [[ $foo == *bar* ]] and [[ $foo =~ .*bar.* ]]. The first uses a glob, the second uses a regex. Just use [[ ! $filename == *myfile* ]]

Upvotes: 1

Related Questions