Thread View: comp.unix.shell
22 messages
22 total messages
Started by Ger Timmens
Tue, 20 Nov 1990 18:37
testing if a file is present
Author: Ger Timmens
Date: Tue, 20 Nov 1990 18:37
Date: Tue, 20 Nov 1990 18:37
10 lines
602 bytes
602 bytes
Can anybody tell me how I can verify whether a file exists or not ? I want to move files and only when they exist ! mv "file" gives an error message if "file" not exists Thanks, Ger Timmens, ger@cv.ruu.nl
Re: testing if a file is present
Author: Ian Watson
Date: Wed, 21 Nov 1990 14:08
Date: Wed, 21 Nov 1990 14:08
18 lines
1083 bytes
1083 bytes
For the Bourne shell, try : if [ -f file ] ; then mv file otherfile fi see test(1) for more details and other useful options. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Ian Watson, HP Pinewood Information Systems Division, England. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Phone : (Intl)+44 344 763015 Unix mail (Internet) : ian@hpopd.HP.COM Unix mail (UUCP) : ...!hplabs!hpopd!ian Openmail : ian watson/pinewood,lab,hpopd Openmail from Unix : ian_watson/pinewood_lab_hpopd@hpopd HPDesk : Ian WATSON/HP1600 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Re: testing if a file is present
Author: David I. Berg
Date: Wed, 21 Nov 1990 15:51
Date: Wed, 21 Nov 1990 15:51
13 lines
941 bytes
941 bytes
In article <ger.659126250@prisma> ger@prisma.cv.ruu.nl (Ger Timmens) writes: >Can anybody tell me how I can verify whether a file exists or not ? >I want to move files and only when they exist ! mv "file" gives an >error message if "file" not exists if (-f filename) will return true if the file exists, false if not. There are many other options for which you can also test: -d if filename is a directory, -z if filename is zero length, -w if filename is writable, etc. RTFM!
Re: testing if a file is present
Author: Jonathan I. Kamens
Date: Wed, 21 Nov 1990 19:16
Date: Wed, 21 Nov 1990 19:16
38 lines
1772 bytes
1772 bytes
In article <ger.659126250@prisma>, ger@prisma.cv.ruu.nl (Ger Timmens) writes: |> Can anybody tell me how I can verify |> whether a file exists or not ? |> I want to move files and only when |> they exist ! mv "file" gives an |> error message if "file" not exists It depends on the shell you're using. In csh or tcsh: if (-e filename) then ... stuff if filename exists ... endif In sh, with either a test built-in named "[" or a link in your filesystem called "[" pointing to the test program: if [ -r filename ]; then ... stuff if filename exists ... fi Some versions of test allow "-e" to test for existence, instead of "-r" to test for existence and readibility, but mine doesn't, so I use "-r" instead of "-e" above. In sh without "[": if test -r filename; then ... stuff if filename exists fi Ksh and bash probably do things the same way sh does, using "[". In the future, it would help if you would give more details, such as the shell you're using, when you ask questions like this. -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710
Re: testing if a file is present
Author: Bill Irwin
Date: Wed, 21 Nov 1990 23:02
Date: Wed, 21 Nov 1990 23:02
28 lines
1310 bytes
1310 bytes
ger@prisma.cv.ruu.nl (Ger Timmens) writes:
>Can anybody tell me how I can verify
>whether a file exists or not ?
>I want to move files and only when
>they exist ! mv "file" gives an
>error message if "file" not exists
Try this:
if [ -f file_name ]
then
mv file_name /new/destination/file_name
fi
The "-f" tests for the existence of the file. There are other options:
-x is the file executable
-r is the file readable
-s does the file have anything in it (greater than zero bytes)
There are others too. Check your manual under "sh". The options should be
listed.
--
Bill Irwin - The Westrheim Group - Vancouver, BC, Canada
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uunet!van-bc!twg!bill (604) 431-9600 (voice) | UNIX Systems
bill@twg.bc.ca (604) 430-4329 (fax) | Integration
Question about 'test' (Was Re: testing if a file is present)
Author: Thomas Heller
Date: Thu, 22 Nov 1990 18:52
Date: Thu, 22 Nov 1990 18:52
14 lines
1066 bytes
1066 bytes
In article <1990Nov21.191638.19469@athena.mit.edu>, jik@athena.mit.edu (Jonathan
I. Kamens) says:
>
>In sh, with either a test built-in named "[" or a link in your filesystem
^^^^^^^^^^^^^^^^^^^^^^^^^^^
>called "[" pointing to the test program:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
But what will the (bourne-) shell say about...
>
> if [ -r filename ]; then
^
... ?
Thomas Heller
Re: testing if a file is present
Author: Wm E. Davidsen Jr
Date: Fri, 23 Nov 1990 03:07
Date: Fri, 23 Nov 1990 03:07
26 lines
1430 bytes
1430 bytes
In article <1990Nov21.191638.19469@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
| In sh, with either a test built-in named "[" or a link in your filesystem
| called "[" pointing to the test program:
|
| if [ -r filename ]; then
| ... stuff if filename exists ...
| fi
|
| Some versions of test allow "-e" to test for existence, instead of "-r" to
| test for existence and readibility, but mine doesn't, so I use "-r" instead of
| "-e" above. In sh without "[":
|
| if test -r filename; then
| ... stuff if filename exists
| fi
Close but not correct.
-f is there a file
-r is it readable
-x is it executable
--
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
sysop *IX BBS and Public Access UNIX
moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me
Re: testing if a file is present
Author: Wm E. Davidsen Jr
Date: Fri, 23 Nov 1990 03:08
Date: Fri, 23 Nov 1990 03:08
11 lines
1025 bytes
1025 bytes
In article <1990Nov21.155149.3505@informix.com> dberg@informix.com (David I. Berg) writes:
| -z if filename is zero length,
Actually -z (zero length) and -n (non-zero length) apply to any string
following the operator, although it certainly applies to filename in strings.
--
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
sysop *IX BBS and Public Access UNIX
moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me
Re: Question about 'test' (Was Re: testing if a file is present)
Author: Eduardo Krell
Date: Fri, 23 Nov 1990 03:15
Date: Fri, 23 Nov 1990 03:15
14 lines
1089 bytes
1089 bytes
In article <90326.175421PPH93@DMSWWU1A.BITNET> PPH93@DMSWWU1A.BITNET (Thomas Heller) writes:
>But what will the (bourne-) shell say about...
>>
>> if [ -r filename ]; then
> ^
Why should the shell do anything about it? The "]" is an argument to
"[" (which is a link to test). The shell just passes that token as an
argument when it exec's test.
Eduardo Krell AT&T Bell Laboratories, Murray Hill, NJ
UUCP: {att,decvax,ucbvax}!ulysses!ekrell Internet: ekrell@ulysses.att.com
Re: testing if a file is present
Author: Jonathan Stigelman
Date: Sun, 25 Nov 1990 19:20
Date: Sun, 25 Nov 1990 19:20
14 lines
898 bytes
898 bytes
There's a one line solution that works with both csh & sh... and is particularly handy in makefiles. [ -f foo ] && mv foo bar || echo foo does not exist This is a handy way to deal with the yacc tokens file y.tab.h so that you don't fire lots of extra recompilataions when everything depends on that tokens file. cmp y.tab.h tokens.h && rm y.tab.h || mv y.tab.h tokens.h Cryptic enough? Stig
Re: testing if a file is present
Author: Jon H. LaBadie
Date: Tue, 27 Nov 1990 07:56
Date: Tue, 27 Nov 1990 07:56
35 lines
1509 bytes
1509 bytes
> In article <ger.659126250@prisma> ger@prisma.cv.ruu.nl (Ger Timmens) writes:
> Can anybody tell me how I can verify whether a file exists or not ?
> I want to move files and only when they exist ! mv "file" gives an
> error message if "file" not exists
A number of poster have responded to Ger's posting by suggesting the
-f option of test (builtin or separate executable). Most claimed this
option tests if the file exists and suggest that the manual be read.
They should read the manual which says:
-f file true if file exists AND is a regular file
(emphasis mine)
I.e., the -f is a filetype/existance test. If a directory foobar
exists, test -f foobar is false; test -d foobar is true.
There are a number of filetype/existance options to test:
-f regular files
-d directory files
-b block special files
-c character special files
-p named pipe files (fifo's)
and on some systems:
-???? symbolic link files
Jon
--
Jon LaBadie
{att, princeton, bcr, attmail!auxnj}!jonlab!jon
Re: testing if a file is present
Author: Geoff Clare
Date: Tue, 27 Nov 1990 13:55
Date: Tue, 27 Nov 1990 13:55
19 lines
1256 bytes
1256 bytes
In <1990Nov21.155149.3505@informix.com> dberg@informix.com (David I. Berg) writes: >if (-f filename) will return true if the file exists, false if not. Wrong. It will return true if the file exists AND IS A PLAIN FILE. To test for existence only use -e. This is one place where csh has an advantage over Bourne shell. There is no equivalent way with the Bourne shell "test" command to test for existence without also testing for a specific file type or access mode. OK, so you can do it with test -f file -o -d file -o -c file -o -b file -o -p file but I don't count that as "equivalent"! -- Geoff Clare <gwc@root.co.uk> (Dumb American mailers: ...!uunet!root.co.uk!gwc) UniSoft Limited, Hayne Street, London EC1A 9HH, England. Tel: +44-71-315-6600
Re: testing if a file is present
Author: Don Bolton
Date: Tue, 27 Nov 1990 22:50
Date: Tue, 27 Nov 1990 22:50
42 lines
1747 bytes
1747 bytes
In article <2356@sixhub.UUCP> davidsen@sixhub.UUCP (bill davidsen) writes:
>In article <1990Nov21.191638.19469@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
>
>| In sh, with either a test built-in named "[" or a link in your filesystem
>| called "[" pointing to the test program:
>|
>| if [ -r filename ]; then
>| ... stuff if filename exists ...
>| fi
>|
>| Some versions of test allow "-e" to test for existence, instead of "-r" to
>| test for existence and readibility, but mine doesn't, so I use "-r" instead of
>| "-e" above. In sh without "[":
>|
>| if test -r filename; then
>| ... stuff if filename exists
>| fi
>
> Close but not correct.
> -f is there a file
> -r is it readable
> -x is it executable
-s file has nonzero length
>--
>bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
> sysop *IX BBS and Public Access UNIX
> moderator of comp.binaries.ibm.pc and 80386 mailing list
if [ -s $USER.IQ ]
then
echo "Proceed..."
read chore
etc etc
else
echo "Stupidity, like virtue, is its own reward"
exit 0
fi
There, now your .sig is more fitting :-)
Re: testing if a file is present
Author: Leslie Mikesell
Date: Wed, 28 Nov 1990 15:18
Date: Wed, 28 Nov 1990 15:18
17 lines
1138 bytes
1138 bytes
In article <ger.659126250@prisma> ger@prisma.cv.ruu.nl (Ger Timmens) writes: >Can anybody tell me how I can verify >whether a file exists or not ? >I want to move files and only when >they exist ! mv "file" gives an >error message if "file" not exists Ah, the error message is the real problem here - just throw it away! mv old new 2>/dev/null Note of course that you also lose "real" error messages with this approach. However, also keep in mind that no method of testing before the move is atomic with the mv itself and thus will fail with multiple concurrent processes. Les Mikesell les@chinet.chi.il.us
Re: Question about 'test' (Was Re: testing if a file is present)
Author: Rob Lemley
Date: Wed, 28 Nov 1990 20:35
Date: Wed, 28 Nov 1990 20:35
11 lines
1006 bytes
1006 bytes
In <14048@ulysses.att.com> ekrell@ulysses.att.com (Eduardo Krell) writes: >Why should the shell do anything about it? The "]" is an argument to >"[" (which is a link to test). The shell just passes that token as an >argument when it exec's test. Also, the test command looks at argv[0] to see if it was called "test" or "[". If "[" then test verifies that the closing "]" argument is there. If the closing "]" is not there, test exits with a status of 1 (false). Rob
Re: Question about 'test' (Was Re: testing if a file is present)
Author: Guy Harris
Date: Thu, 29 Nov 1990 19:04
Date: Thu, 29 Nov 1990 19:04
11 lines
988 bytes
988 bytes
>But what will the (bourne-) shell say about... >> >> if [ -r filename ]; then It'll say "OK, this 'if' is going to run the command named '[', passing it '-r' as its first argument, 'filename' as its second argument, and ']' as its third argument." "test"/"[" will then say "well, I was invoked as '[' rather than 'test'; that means I should check to make sure my last argument is ']', and complain otherwise."
Re: Question about 'test' (Was Re: testing if a file is present)
Author: q.p.liu
Date: Fri, 30 Nov 1990 01:53
Date: Fri, 30 Nov 1990 01:53
10 lines
1080 bytes
1080 bytes
In article <4586@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes: >>But what will the (bourne-) shell say about... >>> >>> If [ -r filename ]; then >"test"/"[" will then say "well, I was invoked as '[' rather than 'test'; >that means I should check to make sure my last argument is ']', and >complain otherwise." trying /bin/[ shows that it only checks if argv[0] == "[".
Re: testing if a file is present
Author: Jon H. LaBadie
Date: Fri, 30 Nov 1990 19:26
Date: Fri, 30 Nov 1990 19:26
23 lines
1126 bytes
1126 bytes
In article <2549@root44.co.uk>, gwc@root.co.uk (Geoff Clare) writes:
>
> test -f file -o -d file -o -c file -o -b file -o -p file
>
> but I don't count that as "equivalent"!
>
OK, granted, but how about the nearly equivalent
test ! -f file
It will be true when you want a regular file of the indicated name
and false if said file does not exist or is a type other than regular.
Another possibility not mentioned in this thread (and with its own
limitations) is test -s file. For this to be true, the file must
exist and have some content (i.e. not an empty file).
Jon
--
Jon LaBadie
{att, princeton, bcr, attmail!auxnj}!jonlab!jon
Re: Question about 'test'
Author: q.p.liu
Date: Sat, 01 Dec 1990 01:27
Date: Sat, 01 Dec 1990 01:27
18 lines
1290 bytes
1290 bytes
In article <1779@seti.inria.fr> deschamp@minos.inria.fr (Philippe Deschamp) writes: >In article <QPLIU.90Nov29205347@phoenix.princeton.edu>, >qpliu@phoenix.princeton.edu (q.p.liu) writes: >|> trying /bin/[ shows that it only checks if argv[0] == "[". > *Never* generalize :-). On my workstation (Sun 3/60 under SunOS 4.0.3): (Ok, was on sunos 4.1, 4.0.3 (sun4) and ctss (cray xmp, cray 2) running sh) (btw, the string comparision is as in awk, not as in C) >What I do not understand is this: > $ /bin/[ > $ > >Why no complaint in the last case ? In this case, argv[0] != "[", hence no check for ].
Re: Question about 'test' (Was Re: testing if a file is present)
Author: Jonathan I. Kamens
Date: Sun, 02 Dec 1990 21:01
Date: Sun, 02 Dec 1990 21:01
23 lines
1428 bytes
1428 bytes
In article <QPLIU.90Nov29205347@phoenix.princeton.edu>, qpliu@phoenix.princeton.edu (q.p.liu) writes: |> In article <4586@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes: |> >"test"/"[" will then say "well, I was invoked as '[' rather than 'test'; |> >that means I should check to make sure my last argument is ']', and |> >complain otherwise." |> trying /bin/[ shows that it only checks if argv[0] == "[". Then either you did something wrong, or you're using a buggy version of "test." $ [ frep test: ] missing $ echo $? 255 $ [ frep ] $ echo $? 0 -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710
Re: Question about 'test' (Was Re: testing if a file is present)
Author: Jonathan I. Kamens
Date: Sun, 02 Dec 1990 21:15
Date: Sun, 02 Dec 1990 21:15
22 lines
1733 bytes
1733 bytes
In article <1990Dec2.210141.13170@athena.mit.edu>, I write: |> In article <QPLIU.90Nov29205347@phoenix.princeton.edu>, qpliu@phoenix.princeton.edu (q.p.liu) writes: |> |> trying /bin/[ shows that it only checks if argv[0] == "[". |> |> Then either you did something wrong, or you're using a buggy version of |> "test." I corrected q.p.liu because I misunderstood what he was saying, and just realized this, so I'd like to apologize. He was saying, "`Test' only checks if the final `]' is present if it is invoked with argv[0] exactly equal to `['." I thought he was saying, "`Test' only checks if its argv[0] is exactly equal to `['; it does not also check if the final ']' is present." Liu is right, of course. Whether or not this behavior of "test" (i.e. the distinguishing between "[" and "/bin/[") is correct is debatable, but I believe that it is. -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710
Re: Question about 'test'
Author: Martin Weitzel
Date: Mon, 03 Dec 1990 13:30
Date: Mon, 03 Dec 1990 13:30
38 lines
1546 bytes
1546 bytes
In article <1779@seti.inria.fr> deschamp@seti.inria.fr writes:
:
:In article <QPLIU.90Nov29205347@phoenix.princeton.edu>,
:qpliu@phoenix.princeton.edu (q.p.liu) writes:
:
:|> trying /bin/[ shows that it only checks if argv[0] == "[".
:
: *Never* generalize :-). On my workstation (Sun 3/60 under SunOS 4.0.3):
: $ test
: $ [
: test: ] missing
:
:What I do not understand is this:
: $ '['
: test: ] missing
: $ PATH=/bin '['
: test: ] missing
: $ /bin/[
: $
:
:Why no complaint in the last case ?
This shows exactly what original poster tried to say (as I understood).
To make it a bit clearer, the source-code of "test" must look like
if (!strcmp(argv[0], "[")) {
if (strcmp(argv[--argc], "]") {
/* complain about missing "]" and exit */
.....
}
}
/* parse arguments; run tests as required */
.....
In your last experiment argv[0] is "/bin/[" and not simply "[", hence no
comlaint.
--
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads