[darcs-users] Re: execute permission for scripts

Ralph Corderoy ralph at inputplus.co.uk
Fri Nov 26 12:04:59 UTC 2004


Hi Norbert,

> One nice and simple way that has proven rather useful for me is the
> one-liner
> 
>  chmod a+x `egrep -r -l '^\#!' .`

Good idea.  Is it worth checking just the magic word instead though as
it won't match /^#!/ elsewhere in the file.

    find . -type f -print |
    while read f; do
        test "`
            dd if=$f bs=2 count=1 2>/dev/null |
            tr -c '#!' xx;
            echo
        `" = '#!' &&
        echo $f
    done |
    xargs chmod a+x

> placed somewhere in a script. For me it works better than setting the
> execution flags by hand, since - without the "#!/bin/sominterpreter"
> line in the beginning - unix cannot execute a script anyway,

A script doesn't need to start with #! to be executed.

    $ echo date >j
    $ chmod +x j
    $ ./j
    2004-11-26 11:41:32 +0000 Fri
    $ strace -f -e trace=execve bash -c ./j
    execve("/bin/bash", ["bash", "-c", "./j"], [/* 30 vars */]) = 0
    execve("./j", ["./j"], [/* 29 vars */]) = -1 ENOEXEC (Exec format error)
    [pid 12905] execve("/home/ralph/bin/date", ["date"], [/* 29 vars */]) = 0
    [pid 12905] execve("/bin/date", ["/bin/date", "+%Y-%m-%d %T %z %a"], [/* 29 vars */]) = 0
    2004-11-26 11:42:08 +0000 Fri
    --- SIGCHLD (Child exited) ---
    $ 

A !# path doesn't have to be absolute either.

    $ cat foo
    #! bar -x
    echo args: "$@"
    $ ls -l bar
    lrwxrwxrwx    1 ralph    ralph           9 Nov 26 11:03 bar -> /bin/bash
    $ ./foo darcs
    + echo args: darcs
    args: darcs
    $ 

Cheers,


Ralph.





More information about the darcs-users mailing list