LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-18-2006, 08:01 AM   #1
rignes
Member
 
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155

Rep: Reputation: 30
bash - Checking for file existance?


I need to check for the existence of jpg, gif, and png files in a directory and if none exist I need to exit with an error. The existence check needs to be case insensitive so that it will find JPG or jpg for example.

I tried:

shopt -s nocaseglob
if [ ! -e *jpg && ! -e *gif && ! -e *png ]; then
echo "No images files found."
exit 1
fi

This isn't catching that no images are in the directory and the script just keeps running, giving tons of errors because the rest of the script relies on the existence of these image files.

I've been thinking over this for about an hour and tried several other things that gave the same result. What am I doing wrong?

Thanks.


Last edited by rignes; 02-18-2006 at 08:06 AM.
 
Old 02-18-2006, 08:06 AM   #2
rignes
Member
 
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155

Original Poster
Rep: Reputation: 30
Wow, am I dumb! I just realised my error. I should be doing:

shopt -s nocaseglob
if [ ! -e *jpg ] && [ ! -e *gif ] && [ ! -e *png ]; then
echo "No images files found."
exit 1
fi

However, now when there ARE images present I get this error when the script hits that line:

/home/rignes/bin/gal: line 42: [: too many arguments

What does this mean? And why would in not error out when no images are present but only when images are?

I've just become very confused.

Last edited by rignes; 02-18-2006 at 08:10 AM.
 
Old 02-18-2006, 12:47 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
perhaps just considering doing something like

Code:
if [ "`ls | grep -i '\(\.gif\|\.jpg\|\.png\)' | wc -l`" -eq 0 ]; then
 echo "No image files found."
 exit 1
fi
Using grep w/ the -i option has the benefit of being case insensitive.
 
Old 02-18-2006, 12:48 PM   #4
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
[ is actually a symbolic link to "test" program (man test). Test can only check for the existece of ONE file at the time. The problem with your code is that when there are several matches for (for example) *jpg, the shell expands *jpg to ALL the matching files causing test to get too many arguments (more than one filename), just like it says.

--edit--

The solution suggested by 95se seems pretty elegant to me. You might want to use the "end-anchor" ($) in your grep statement to make sure that it only matches the files that end with .gif, .jpg or .png, instead of matching every filename that has the string. It would make it:

Code:
grep -i '\(\.gif\|\.jpg\|\.png\)$'

Last edited by alienDog; 02-18-2006 at 12:58 PM.
 
Old 02-18-2006, 01:09 PM   #5
rignes
Member
 
Registered: Mar 2003
Location: USA
Distribution: Slackware-current
Posts: 155

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by 95se
perhaps just considering doing something like

Code:
if [ "`ls | grep -i '\(\.gif\|\.jpg\|\.png\)' | wc -l`" -eq 0 ]; then
 echo "No image files found."
 exit 1
fi
Using grep w/ the -i option has the benefit of being case insensitive.
Actually, I came up with something similar since I last posted. But your way if better than mine. Thanks!
 
Old 02-18-2006, 04:02 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I prefer 'find' because it will search recursively and you can control the file types:
Code:
#You need to filter out irregular files such as directories and devices
if [ -z "`find -type f -name "*\.jpg" -name "*\.gif" -name "*\.png"`" ]; then
  echo "ERROR!"
  exit
else
  #...
fi
ta0kira

Last edited by ta0kira; 02-18-2006 at 04:06 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
checking sha1 file trueromance Fedora - Installation 3 12-25-2005 12:17 PM
stop bash checking mails at login phil.d.g Linux - General 2 09-09-2005 08:33 AM
Simple bash problem - checking for files and displaying the first line morrolan Programming 4 03-07-2005 11:40 AM
Checking for a valid HTML link in Bash/Korn? Aziz Programming 1 11-30-2004 05:10 PM
Checking if a file exists in C? tristan_vdv Programming 3 05-24-2002 11:06 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration