The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl: When dealing with files that do not exist joeyg Shell Programming and Scripting 2 02-20-2008 02:09 PM
Check if certain files exist in a directory, if not add name to a textfile SunnyK Shell Programming and Scripting 1 02-07-2008 06:21 AM
How to check a file exist and do a copy of other files ahjiefreak Shell Programming and Scripting 6 12-19-2007 09:07 PM
Tab Completion showing files that Dont Exist dittonamed UNIX for Advanced & Expert Users 3 05-27-2003 09:50 AM
Files listed Do not really exist Ricky Raynor UNIX for Advanced & Expert Users 6 02-08-2002 02:20 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-13-2008
Registered User
 

Join Date: Apr 2008
Posts: 13
testing if files exist

I am trying to test arguments to see if they are files in any directory.

I have :
[ $# -f 0 ]

but it's not working
Reply With Quote
Forum Sponsor
  #2  
Old 04-13-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Is your script supposed to be invoked with the files you want to process, and terminate if they don't exist? $# indicates how many arguments there were (presumably you want to abort if there were none) but to check whether the named file(s) exist, you give them as arguments to "test -f".

Code:
case $# in 0) echo no files, try again >&2 ;; esac
for f in "$@"; do
  if [ -f "$f" ] ; then
    ... handle file
  else
    echo "$0: file $f not found -- skipping" >&2
  fi
done
Reply With Quote
  #3  
Old 04-13-2008
Registered User
 

Join Date: Apr 2008
Posts: 13
when I run my program and enter 2 arguments at the command line it is not checking to see if the 2 arguments are files or not.

I have tried

if [ -f filename ]
elif [ -f $1 -o $2 ]

I guess I don't know how to make it so it uses the 2 arguments as filenames and then searches for them.
Reply With Quote
  #4  
Old 04-13-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
That's the loop. If you always want exactly two arguments and they both need to exist, you can do that too. The -o doesn't "remember" what you did before so you need to tell it again to look for a file.

Code:
if [ -f "$1" -o -f "$2" ]
The loop I posted earlier works for any number of file names, and checks each of them in turn; but if two need to exist at the same time, it won't check for that. (Of course, then you need -a "and", not -o "or".)
Reply With Quote
  #5  
Old 04-30-2008
Registered User
 

Join Date: Apr 2007
Posts: 18
How to check if a file exists

I need to check following three things:

a) a file exists but is zero byte
b) a file exists with non-zero bytes
c) a file doesn't exist at all

What switches do we use? Is it -f, -s, -z. Please explain the difference in these!

Many Thanks
Shalua
Reply With Quote
  #6  
Old 04-30-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
You know enough to be better served by finding the documentation and reading that. It's more precise and more authoritative than any of us here, on a typical day. Read man sh and maybe man test if you have that.
Reply With Quote
  #7  
Old 05-01-2008
Registered User
 

Join Date: Apr 2007
Posts: 18
I am looking for an answer to my question. Any pointers or answers will be appreciated.

Thanks
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0