LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   if test for a file name pattern (https://www.linuxquestions.org/questions/programming-9/if-test-for-a-file-name-pattern-393128/)

Melsync 12-15-2005 08:19 AM

if test for a file name pattern
 
I'm sorry if this is too basic.
I need to pin some files down in a directory to make a bash script with them.
I want to select the file containing '*-reduced.html' string in its name among these ones:
200512-reduced.html
200512-reduced.jpg
200512-reduced.zip
200512-snapshot.html
200512-snapshot.jpg
200512-snapshot.zip
and many others like 200511-another.txt, etc.

$ snp=*-reduced.html
$ echo $snp
200512-reduced.html
$ if [[ -e $snp ]]; then echo "html exits"; else echo "no html"; fi
no html


Why? The variable is populated with the name of a file!

I have no idea of perl to deal with regular expressions, so I'd prefer your help in bash shell only :) so I can progress from there :)
Thanks.

crabboy 12-15-2005 08:34 AM

Code:

#!/bin/sh

START_LOCATION=/home
FILE_PATTERN='*-reduced.html'

# Search a directory tree
for i in `find $START_LOCATION -name $FILE_PATTERN `; do

  echo $i

done

# Search a single directory

for i in `ls $START_LOCATION/$FILE_PATTERN`; do
  echo $i
done


crabboy 12-15-2005 08:36 AM

Reading through you post again, I'm not sure what you are after. Do you just want a yes/no if the file of that pattern exists in a directory?

Melsync 12-15-2005 08:57 AM

Thanks.
No, I'm not after a boolean; I need to do all sorts of things (basename, rename, zip, rm, etc.) to that precise file while keeping all the other files in the directory unchanged.
I need to be able to do for instance
zip $(basename $FILE_PATTERN .html).zip $FILE_PATTERN
in a loop and then move on to the next file manipulation loop like
FILE_ANOTHER_PATTERN='*-snapshot.html'
for FILE_ANOTHER_PATTERN in *
do

Melsync 12-15-2005 08:59 AM

sorry, I hit the 'button' post too soon:

for FILE_ANOTHER_PATTERN in *
do
rename $(basename $FILE_ANOTHER_PATTERN -snapshot.html) -reduced.html
done

bigearsbilly 12-15-2005 09:10 AM

simpler:
Code:

if [ -e *.html ];then
  blah blah
fi


bigearsbilly 12-15-2005 09:15 AM

doh!
the above doesn't work in bash
crappy bash.




otherwise,
is it too many [[]]
try
[ -e "$snp" ]

crabboy 12-15-2005 09:15 AM

Code:

#!/bin/sh

START_LOCATION=/home
FILE_PATTERNS="*-reduced.html \
              *-whatever.html \
              *-linux.html"

# Search a directory tree
for PAT in $FILE_PATTERNS; do
  echo "Processing pattern $PAT"
  for i in `find $START_LOCATION -name $PAT `; do
      echo Processing file $i
      # gzip $i
  done
done


Melsync 12-15-2005 09:49 AM

Quote:

is it too many [[]]
try
[ -e "$snp" ]
Yes, it worked!

Melsync 12-15-2005 09:51 AM

Quote:

FILE_PATTERNS="*-reduced.html \
*-whatever.html \
*-linux.html"
did the trick as well.

Thanks x10 guys!

P.S.
I changed #!/bin/sh to #!/bin/bash in the script and it worked as well.
Why so much irritation about bash? I don't know anything about other shells!

Melsync 12-15-2005 09:53 AM

By 'irritation' I don't mean in this post, but in other posts I read.

crabboy 12-15-2005 10:48 AM

bash is the best of the bunch and far better than the putrid shell that MS tries to pass off.

chrism01 12-15-2005 05:42 PM

bash is the default shell in Linux, capabilities based on commercial ksh which is common on commercial Unices (sic).
It only appears to be irritating (here) because it's a programming lang, but newer people tend to hope that doing what seems simple to a human is so to a computer. It ain't so. ;)
IOW, all langs take some time to learn....


All times are GMT -5. The time now is 10:59 PM.