![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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" ] |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
|||
|
|||
|
I am looking for an answer to my question. Any pointers or answers will be appreciated.
Thanks |
|||
| Google The UNIX and Linux Forums |