2

Is there a programmatic way to find out why I can't write to a file in a shell script (file exists, access denied, filesystem full, etc)?

For example in C one uses the errno variable for this:

FILE *fp = fopen("/etc/passwd", "w");
if (!fp) {
    if (errno == EACCES) {
        // ...
    }
    else if (errno == EEXISTS) {
        // ...
    }
}

Can I do something similar with shell scripting?

My test script:

#!/bin/sh

err=$(echo 'x' 2>&1 > /etc/passwd)
if [ $? -ne 0 ]; then
    echo "An error occured: $err"
    exit 1
fi

Running this, as a non-root user, will give you:

An error occured: 'test.sh: line 3: /etc/passwd: Permission denied'

Checking the contents of $err is not a very good solution IMHO; the error message may change in the future, and is dependent on the user's locale settings.

I also know I can do a stat & some other checks before creating a file, or after the exit code is non-zero; but this quickly gets out of hand if you want to check all possible errors. In some cases, it's also susceptible to race conditions (such as the file being creating between your test -f and actually writing it).

I'm using the POSIX shell, and would prefer to do it in a POSIX-compliant way, but a bash or zsh extension is also acceptable.

1
  • 4
    $? is the exit/error code. that's about all you'll get other than the actual string output you're capturing.
    – Marc B
    Nov 26, 2014 at 14:56

2 Answers 2

0

Most programs your command is going to call catch the error, interpret the errno and print a descriptive (but not machine-readable) error message. So the formal information of errno normally is lost; all you can do is interpret the human-readable error message.

On the other hand, the actual reason for a failure of an arbitrary command is subject to an intelligence, and unless you want to program an artificial one for this, you will not succeed. If a write on file /a/b/c/d fails, this can be because d has no write permission bits set, or because any of a, b, c do not have execute permissions, or because there is an I/O Error during writing, or because the file system is mounted as read-only. And I'm sure I just forgot some more possibilities. And this was just for a very simple write on a file. If you try to pull this straight for arbitrary commands, it will be way too much.

For mere file access topics (based on permissions) you can get somewhere by using faccessat(2) (but I'm not aware of a shell-interface to that).

0

There's several OS built-in functions to check special features of a file:

it exist but you can't write/read to it - it's 0 bytes - it's a directory (!) - it's a symbolic link - and some more

Check via

man test

or have a quick check via: http://unixhelp.ed.ac.uk/CGI/man-cgi?test

In addition, you can for example check if the directory of the file itself, exists. That may be a reason why the file does not exist. You can use DIRNAME for that:

http://linux.die.net/man/1/dirname

2
  • Thanks; as I mentioned in my question, this is not what I would like to do: I also know I can do a stat & some other checks before creating a file, or after the exit code is non-zero; but this quickly gets out of hand if you want to check all possible errors. In some cases, it's also susceptible to race conditions (such as the file being creating between your test -f and actually writing it). Nov 27, 2014 at 16:23
  • Yes, all these conditions may change at any time, that may also be something to keep in mind. A file can also be open (in use by a process) or not. What you also can do is make a checksum (crc/cksum) to track changes to a file.
    – tvCa
    Nov 28, 2014 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.