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.htmland 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. |
Code:
#!/bin/sh |
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?
|
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 |
sorry, I hit the 'button' post too soon:
for FILE_ANOTHER_PATTERN in * do rename $(basename $FILE_ANOTHER_PATTERN -snapshot.html) -reduced.html done |
simpler:
Code:
if [ -e *.html ];then |
doh!
the above doesn't work in bash crappy bash. otherwise, is it too many [[]] try [ -e "$snp" ] |
Code:
#!/bin/sh |
Quote:
|
Quote:
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! |
By 'irritation' I don't mean in this post, but in other posts I read.
|
bash is the best of the bunch and far better than the putrid shell that MS tries to pass off.
|
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. |