[prog] check if script parent process is "init"

John Clarke johnc+linuxchix at kirriwa.net
Wed Apr 21 18:33:46 EST 2004

On Wed, Apr 21, 2004 at 08:35:35AM +0200, dominik schramm wrote:

> $ cat caller.sh
> #!/bin/sh
> . callee
> $ cat callee
> #!/bin/bash
> # contains bash specific syntax
> ...
> My thought was that this couldn't work.

It'll work fine, because the kernel uses the first line of the script
(the part after "#!") to find the handler.  caller.sh can only contain
sh commands because it's being run by /bin/sh, but callee is being run
by /bin/bash so it can include anything that bash understands. 

The kernel reads the first line; if it starts with "#!", it gets the
name of the program that follows and runs that program, with up to (I
think) two arguments plus the name of the file.  It's simple enough to
write your own handler:

    [johnc at dropbear ~/tmp]$ cat echo.c
    #define _GNU_SOURCE
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
        FILE *f;
        char line[1024];

        if (argc < 1)

        f = fopen(argv[1],  "r");
        if (f == NULL)
            printf("Cannot open %s: %s\n", argv[1], sys_errlist[errno]);
        while (fgets(line, sizeof(line), f) != NULL)
            if (line[0] != '#')
                printf("%s", line);
        return 0;
    [johnc at dropbear ~/tmp]$ gcc -W -Wall -std=c99 -Wno-unused-parameter -o echo echo.c
    [johnc at dropbear ~/tmp]$ cat echo.sh 
    Hello World
    [johnc at dropbear ~/tmp]$ ./echo.sh 
    Hello World



> ... In the mean time, suggestions are welcomed as to what to do to the 
> buffoon who wrote the current Virus du Jour.
Do you mean the larger part that was written in Redmond, or the minority
part that someone else contribbed?        -- Anthony de Boer

More information about the Programming mailing list