Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
Homework question? Regardless, you should really show what you've tried so far. –  Dennis Williamson Oct 7 '09 at 13:34

2 Answers 2

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" ]
share|improve this answer
...and for looping on arguments consider shift... –  dmckee Oct 7 '09 at 13:21
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
magic() {
    for p in "$@"; do
        [ -O "$p" -a -x "$p" ] && /bin/sh "$p"

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

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.