Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

To get current user id you do:

id -u

to get owner of file, you do:

stat -c "%u"

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" instead... – May 16 '13 at 14:36

Your Answer


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