Something that would take any number of arguments, where each argument would be a path to a file or directory. If the user does own a particular path then it should check it to see if the path represents a normal file AND if that file is executable. If it is, then your script should execute/run the file.

Thanks for any help I've been trying different things for too long and I'm frustrated.

  • Homework question? Regardless, you should really show what you've tried so far. Oct 7, 2009 at 13:34

3 Answers 3

magic() {
    for p in "$@"; do
        [ -O "$p" -a -x "$p" ] && /bin/sh "$p"

read 'man test' to see what the checks do.

  • 4
    Illegible showing off. Oct 7, 2009 at 14:22
  • @tkadlubo: indeed. part to blame is display of <code> enclosed stuff here on superuser.
    – akira
    Oct 7, 2009 at 14:28

The -O test checks if the current user is the owner of a file or folder.

if [ -O "$FILENAME" ]; then echo 'Owned!'; else echo 'Nope!'; fi

See list of file test operators (and how to do tests in general.)

Using stat on *nix for this purpose is tricky to do portably due to differences between platforms.


To get current user id you do:

id -u

to get owner of file, you do:

stat -c "%u" file.name

to test if one value is the same as the other you do:

if [ "$first" -eq "$second" ]
  • ...and for looping on arguments consider shift... Oct 7, 2009 at 13:21
  • 1
    Note that on OSX (and perhaps other BSD-alikes) you need to use stat -f "%u" file.name instead... May 16, 2013 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy