sh version of DOS "if exist"

sh version of DOS "if exist"

Post by Dana Frenc » Sun, 03 Dec 1995 04:00:00




>I know it's there somewhere. I just can't see it.

>In DOS, if I wanted to check if a file existed, and if it did, to do
>something, I'd say:

>if not exist f:\path\file.ext goto nothing
>... do things here

The command you are looking for is as follows:

if [ ! -f filename ]

This says "if not an existing regular file"

You need to look at the man page for "test"

man test

Dana French

 
 
 

sh version of DOS "if exist"

Post by Ross Wheele » Sun, 03 Dec 1995 04:00:00


I know it's there somewhere. I just can't see it.

In DOS, if I wanted to check if a file existed, and if it did, to do
something, I'd say:

if not exist f:\path\file.ext goto nothing
... do things here

:nothing
... do other things here

In sh, I can test a file is readable *to me* with:
  if [ -r filename ]; then
       do this
  fi

BUT! How do I check if a file *exists* (regardless of if I can read it or
not)?

Regards, RossW

 
 
 

sh version of DOS "if exist"

Post by Bill Marc » Mon, 04 Dec 1995 04:00:00




Quote:>I know it's there somewhere. I just can't see it.

>In DOS, if I wanted to check if a file existed, and if it did, to do
>something, I'd say:

>if not exist f:\path\file.ext goto nothing
>... do things here

>:nothing
>... do other things here

>In sh, I can test a file is readable *to me* with:
>  if [ -r filename ]; then
>       do this
>  fi

>BUT! How do I check if a file *exists* (regardless of if I can read it or
>not)?

man test
-f filename  exists and is a plain file
-d    "        "     "   " " directory
-s    "        "     "  has size greater than zero
(several other options including -r, -w, -x, etc)

--
"Goodness me, could this be industrial disease?"  Dire Straits

 
 
 

sh version of DOS "if exist"

Post by Nick Ke » Mon, 04 Dec 1995 04:00:00




Quote:>In DOS, if I wanted to check if a file existed, and if it did, to do
>something, I'd say:

>if not exist f:\path\file.ext goto nothing
>.... do things here

>:nothing
>.... do other things here

>BUT! How do I check if a file *exists* (regardless of if I can read it or
>not)?

if [ -e filename ]; then
     do this
else
     do something else
fi

You can use -r to test if it exists _and_ is readable. -f will test if the file
exists and is a regular file.

--
Our continuing mission: To seek out knowledge of C, to explore strange
UNIX commands, and to boldly code where no one has man page 4

 
 
 

sh version of DOS "if exist"

Post by Alain Guisol » Fri, 08 Dec 1995 04:00:00



...

Quote:

> In sh, I can test a file is readable *to me* with:
>   if [ -r filename ]; then
>        do this
>   fi

> BUT! How do I check if a file *exists* (regardless of if I can read it or
> not)?

Try the following:

if [ -f filename ] ; then
        do this
fi

This tests if filename "is a file" (and not a directory)
but could be a solution.

the best is in csh:

if ( -e filename ) then
        do this
endif

Regards, Alain
  _______                                        _________

 \ \ \__\ \ ---------------------------------- / / ______\/
  \ \  ___ \ Your conscience never stops you  / / /__  /\
   \ \ \_/\ \     from doing anything.       / /____/ / /
    \ \_\\ \_\   It just stops you from     /________/ /
     \/_/ \/_/        enjoying it.          \________\/

 
 
 

1. dowhat=`expr "dowhat.sh" : "\(.*\)\.sh$"`;

This is from a Bash shell script.  The purpose is to get at the base
name of the script (which is contained within the script variable $0),
using the regular expression mode of expr.

.* matches 0 to infinity (the *) chars (the .) except for a newline,
and the escaped parentheses are needed to encapsulate that expression
for some reason. \.sh$ specifies ".sh" occurring at the end of a line
(escape needed to specify a literal .).

Please explain how this strips off the .sh extension.  If you enter
dowhat.sh in Emacs, and search using this regular expression, it
happily matches the whole shebang, whereas as used in the script, the
extension is subtracted!

Good regular expressions tutorial:
http://www-105.ibm.com/developerworks/education.nsf/linux-onlinecours...

2. SIIG SCSI Controller SCSI-PRO VL

3. more secure?: "#!/bin/sh -" or "#!/bin/sh"

4. gzip untar in specific directory /temporary

5. Is "sh file" equivalent to "cat file | sh"

6. Another Newbie question , WHAT IS A LINUX BOX

7. GETSERVBYNAME()????????????????????"""""""""""""

8. where can i find ftpserver for solaris x86? pls help me. thanks

9. sh says: test "$1" = "" when $1="-b"

10. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""

11. CygWin - PATH finds "myscript.sh" but not "myscript"

12. ":" v. "#!/bin/sh"

13. make "perl sh_script", "sh sh_script" both work