View Full Version : Testing path for inclusion of sub path
MattUK
September 3rd, 2008, 09:21 PM
I am trying to find a way of reliably checking a path for a substring. This is what I have at the moment:
if [[ ! $destination_dir =~ '\/data\/store\/blarp\/*' ]]; then
Basically the only way I want that if statement passing is if the full path contains /data/store/blarp, but the above doesn't seem to be working consistently for me.
Any help appreciated.
stylishpants
September 3rd, 2008, 09:41 PM
Perl:
fhanson@fhanson:~$ var="/path/good"
fhanson@fhanson:~$ if perl -e 'exit !($ARGV[0] =~ m[/good])' "${var}"; then echo "yes"; else echo "no"; fi
yes
fhanson@fhanson:~$ var="/path/bad"
fhanson@fhanson:~$ if perl -e 'exit !($ARGV[0] =~ m[/good])' "${var}"; then echo "yes"; else echo "no"; fi
no
fhanson@fhanson:~$
The code needs that weird little inversion because the perl match operator returns true/false as 1/0, but bash return codes map true/false to 0/1.
Of course a nice benefit of using perl is that its match operator lets you use something like m[] for delimiters instead of //, meaning that you don't need to escape all the slashes in your path anymore.
MattUK
September 3rd, 2008, 09:54 PM
That does the trick, thanks! Id noticed the weird 0/1 thing with bash, very confusing..
mssever
September 3rd, 2008, 10:02 PM
That does the trick, thanks! Id noticed the weird 0/1 thing with bash, very confusing..
It's because the exit status of a successful program is 0. Most of what you do in bash is run external programs, so you have to cope with their exit statuses.
ghostdog74
September 4th, 2008, 01:36 AM
I am trying to find a way of reliably checking a path for a substring. This is what I have at the moment:
if [[ ! $destination_dir =~ '\/data\/store\/blarp\/*' ]]; then
Basically the only way I want that if statement passing is if the full path contains /data/store/blarp, but the above doesn't seem to be working consistently for me.
Any help appreciated.
you do not have to use any external programs to check path in bash
case $destination_dir in
/data/store/blarb* ) echo "yes";;
* ) echo "no";;
esac
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.