LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2010, 06:16 AM   #1
headhunter_unit23
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
/bin/bash if statement pattern search, end of pattern special character?


Hi,

I have to enhance the behaviour of a backup script written in perl. I don't need to change it, what I need to do is to create a bash script that does some checks like file name and file size, execute the backup script then check if the backup files match the original files.

Here's how I try to do it:

- read the files from the original files folder
- store them in an array
- search in the array the files that have a specific file extension
- store the file names that match the search pattern (I know the backup script skips some files so I can hardcode the search pattern)
- run the backup script
- read the files from the backup folder
- store them in an array
- compare the original files name and size stored in an array with those from the backup folder
- send a report email

The problem:

- search in the array the files that have a specific file extension

here's the code so far:

-------------------------------------------------------------------------

#!/bin/bash

i=0
j=0
k=0

match='.vmx'
match2='.vmxf'

cd /path/to/the/original/files

for f in *; do
files[i++]=${f}
if [[ "$f" =~ .vmdk ]];
then
# echo $f;
vmdktocompare[j++]=${f}
fi
done

for f in *; do
files[i++]=${f}
# if [[ "$f" =~ $match ]];
if [[ "$f" =~ ".vmx" && "$f" != $match2 ]];
then
vmxtocompare[k++]=${f}
fi
done

echo ${vmdktocompare[*]}
echo ${vmxtocompare[*]}

exit 0

-------------------------------------------------------------------------

The output:

# sudo ./listdir.sh
test2.vmdk test.vmdk
test.vmx test.vmxf

As you can see the search pattern ".vmx" is not exactly respected by the system, as for it ".vmx" and ".vmxf" are both matching the search pattern. Which is entirely true.

Does anybody know how to tell the system: search for this: ".vmxAND_STOP_HERE", don't look for ".vmxXXXXXXXXXXXXXXX".

Is there a special character that can tell the system that the pattern stops after ".vmx"? I googled for two days but no luck.

I tought this line should do the trick:

if [[ "$f" =~ ".vmx" && "$f" != $match2 ]];

but apparently, the boolean AND is ignored.

Thanks
 
Old 04-29-2010, 06:29 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Bash's =~ comparison operator takes a regular expression on the right side; you may use $ at the end of that expression to anchor it to the end of the string as in
Code:
if [[ "$f" =~ .vmdk$ ]]
In a regular expression, "." means "any single character". If you want to match "." itself then escape it, hence (not tested)
Code:
if [[ "$f" =~ \.vmdk$ ]]

Last edited by catkin; 04-29-2010 at 06:30 AM. Reason: root->anchor
 
1 members found this post helpful.
Old 04-29-2010, 08:02 AM   #3
headhunter_unit23
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks catkin,

it did the trick!
 
Old 04-29-2010, 08:05 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You're welcome. Glad it worked for you

The thread can be marked SOLVED via the Thread Tools menu.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash search for a pattern within a string variable nutthick Programming 8 03-17-2015 07:26 PM
Pattern matching in a bash case statement using regex ciphyre Programming 1 01-31-2009 12:20 PM
Bash script to search for specific pattern? jimyg10 Programming 22 05-18-2007 01:02 PM
java, search character pattern in string howto xemous Programming 2 08-09-2006 07:53 AM
Pattern matching with a "case" statement in BASH King V Programming 8 04-27-2006 04:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration