Shell Script in c

Embed Size (px)

Citation preview

  • 8/9/2019 Shell Script in c

    1/3

    Example 1

    The following Ch shell script can be used to backup file systems. #include int main () { printf("Hello, world!\n"); echo Ch is cool! I can now use a C program to backup files

    dump 0ubdsf 80 50000 50000 /dev/nrst0 /dev/sd0a } Example 2

    The example below will print out all sub-directories and files in the current directory,as well as the full command path for command gzip.

    #!/bin/ch

    #include

    char *s;

    int i=0;

    foreach (s;`ls`) {

    i++;

    echo $s; }

    printf("the total number of directory and files are %d\n",i);

    s =`which gzip`;

    echo $s;

    Example 3

    In this example, the controlling variable of the switch statement is a string instead ofintegral type.

    #!/bin/ch

    char *ch="SoftIntegration Ch"; float version=2;

    string_t hostname=`hostname`;

    switch (hostname) {

    case "mymachine":

    printf("Hello world\n");

    break;

    case "testmachine":

    fprintf stdout

  • 8/9/2019 Shell Script in c

    2/3

    Example 4

    Below is another sample shell script. #!/bin/ch int main() { char *s = "pointer to char";

    int i; float f, *p; f = 6; p= &f; printf("s = %s\n", s); printf("*p = %f\n", f); /* output from the following statement is "6 6 pointer to char o" */ echo $f $(*p) $s $(*(s+1)); pwd ls > junk } Example 5

    The following shell commands in Bourne Shell sh #!/bin/sh

    command argument

  • 8/9/2019 Shell Script in c

    3/3

    Example 7

    Find and compile all .c files into .o in the current directory when the .o is old or

    absent.

    #!/bin/ch

    #include

    struct stat cstat, ostat;

    string_t c, o;

    foreach (c; `find . -name "*.c"`) {

    o=`echo $c | sed 's/.c$/.o/'`;

    stat(o, &ostat);

    stat(c, &cstat);

    if (ostat.st_mtime > cstat.st_mtime) {

    echo "compiling $c to $o";

    gcc -c -o "$o" "$c";

    }

    }