LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-26-2009, 12:22 AM   #1
Cyberman
Member
 
Registered: Aug 2005
Distribution: Debian Stable
Posts: 218

Rep: Reputation: 17
if-then statement and wildcard: shell script


I have a bunch of .pdf files in a folder.
And if the folder already has .pdf files, then I simply want the file to meow. Otherwise, if it doesn't think .pdf files already exist, then go bark.

There are .pdf files in the folder.

Code:
#!/bin/sh
if [ -e ./*.pdf ]
then
echo "meow"
else
echo "bark"
fi
Quote:
./test: line 2: [: too many arguments
bark
How do I get it to go meow with a wildcard or something similar?
 
Old 12-26-2009, 12:25 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you don't have to do it that way (and its not the right way anyway)
Code:
shopt -s nullglob
for file in *.pdf
do
   if [ -f "$file" ];
     echo "bark"
   fi
done

Last edited by ghostdog74; 12-26-2009 at 12:27 AM.
 
Old 12-26-2009, 08:25 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Try something like this:
Code:
nrfiles=$(find . -maxdepth 1 -name "*.pdf" | wc -l)
if [ $nrfiles -gt 0 ]
then
  meow
else
  bark
fi
The problem with a construct like [ -e *.pdf ] is that the shell actually expands the token *.pdf to foo.pdf, bar.pdf... before it evaluates the expression. So the expression becomes
Code:
if [ -e foo.pdf bar.pdf baz.pdf ]
which is invalid of course.

jlinkels
 
Old 12-26-2009, 09:25 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jlinkels View Post
Try something like this:
Code:
nrfiles=$(find . -maxdepth 1 -name "*.pdf" | wc -l)
another way, w/o external commands.
Code:
[ ! -z "$(echo *pdf)" ] && echo "meow"
 
Old 12-26-2009, 11:08 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by ghostdog74 View Post
Code:
[ ! -z "$(echo *pdf)" ] && echo "meow"
If there are no pdf files present, the result of *pdf is "*pdf". At least in Bash this is true. Therefor the test file never yields false.

For the same reason you cannot use
Code:
for fname in $(ls *pdf)
as when no files are found ls produces an error string.

I have to admit that "echo *pdf" to get a listing of pdf files in the current directory is cool.

jlinkels
 
Old 12-26-2009, 12:11 PM   #6
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
Quote:
Originally Posted by jlinkels View Post
If there are no pdf files present, the result of *pdf is "*pdf". At least in Bash this is true. Therefor the test file never yields false.

For the same reason you cannot use
Code:
for fname in $(ls *pdf)
as when no files are found ls produces an error string.
Unless you use shopt -o nullglob in which case echo *pdf produces a single line-end and ls *pdf produces a listing of the current directory.
 
Old 12-26-2009, 05:17 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jlinkels View Post
If there are no pdf files present, the result of *pdf is "*pdf". At least in Bash this is true. Therefor the test file never yields false.
i had my nullglob turned on by default so i didn't include. yes as catkin mentioned, set nullglob and it should solve the problem.
 
Old 12-27-2009, 07:49 AM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
It works with nullglob enabled. It is the first time I see nullglob being used though. And it was not smart either to disregard the statement in your first example either.

jlinkels
 
  


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
FTP get script wildcard abharsair Linux - Newbie 4 02-16-2009 06:33 AM
for statement in bourne shell script bujecas Linux - General 3 07-17-2006 07:32 AM
How do I use an If statement within a case in a Shell script?? crazygyhrous Programming 7 01-03-2006 06:41 AM
script for a command that won't accept wildcard value hemp4fuel Programming 4 05-24-2004 05:30 AM
cat sh script wildcard problem scm Programming 8 04-21-2004 05:19 AM

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

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

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