2

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. – Dennis Williamson Oct 7 '09 at 13:34
4
magic() {
    for p in "$@"; do
        [ -O "$p" -a -x "$p" ] && /bin/sh "$p"
    done
}

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

  • 3
    Illegible showing off. – Tadeusz A. Kadłubowski Oct 7 '09 at 14:22
  • @tkadlubo: indeed. part to blame is display of <code> enclosed stuff here on superuser. – akira Oct 7 '09 at 14:28
3

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" ]
then
    ....
fi
  • ...and for looping on arguments consider shift... – dmckee Oct 7 '09 at 13:21
  • 1
    Note that on OSX (and perhaps other BSD-alikes) you need to use stat -f "%u" file.name instead... – Irongaze.com May 16 '13 at 14:36
1

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.

Your Answer

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