Re: Acting upon error messages

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 09/21/04


Date: 21 Sep 2004 12:34:43 GMT

2004-09-21, 00:18(-07), Svatoboj:
> Dear users,
>
> as I didn't write so many shell scripts I wonder how to act upon an
> error message?
> Just for example:
>
> --snip--
>
> if [ -e $dir]; then

if [ -e "$dir" ]; then
but it will fail (won't report existence) if "$dir" is a symlink
to an inaccessible file.

> echo "Directory exists"

or file or named pipe, or symlink...

> else
> mkdir $dir

mkdir -- "$dir" && echo success

> echo "Directory succesfully created"
> fi
>
> --snip--
>
> But what about mkdir returns error for whatever reason? Wrong perms or
> so? I would like to print an error message an exit.
> How to achieve this?

if mkdir -- "$dir"; then
  echo "Directory succesfully created"
else
  exit 1
fi

No need to check for file existence, mkdir will do that anyway.

-- 
Stephane


Relevant Pages

  • Re: rm(1) bug, possibly serious
    ... $ mkdir -p foo/var ... $ cd foo/bar ... Looks like you have mistyped 'mkdir' argument:) ... The first time I got an error message, ...
    (freebsd-stable)
  • error: mkdir undeclared
    ... When trying to install a program that is originally made for linux, ... this error message: ... I guess there is a funktion mkdir in unistd.h at linux systems. ...
    (comp.unix.bsd.freebsd.misc)
  • Acting upon error messages
    ... as I didn't write so many shell scripts I wonder how to act upon an ... echo "Directory succesfully created" ... But what about mkdir returns error for whatever reason? ... I would like to print an error message an exit. ...
    (comp.unix.shell)
  • Re: Acting upon error messages
    ... > as I didn't write so many shell scripts I wonder how to act upon an ... > But what about mkdir returns error for whatever reason? ... I would like to print an error message an exit. ...
    (comp.unix.shell)
  • RE: Basic symlink question...
    ... > command to create/update a link on the Unix server. ... description of what actually happened (including the error message you ...
    (perl.dbi.users)