Thread: Mixing BASH with C

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    37

    Mixing BASH with C

    How do you mix BASH with C. What I am trying to do is with a c-program take input into two arrays and then send them into any BASH command and execute the command. I have tried doing it with copy but with no luck.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char name1[20];
    	char name2[20];
    
    	printf("Please enter two names.\n");
    	scanf("%s %s", name1, name2);
    	
    	system("cp name1 name2");
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well I'm no Linux expert, but that system() call? name1 and name2 are parts of a string literal in that parameter. You're not referring to the arrays in that case. Rethink that, and then try again.

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    use the exec function family. 'man 3 exec' should do it.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Do you want to have the two names passed as arguments to the executable, or do you want them to be sent via standard input? You are passing them as arguments, but keep in mind that many unix tools expect the data to arrive via standard input through a pipe redirection...
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The program being executed is cp. cp expects arguments.

    sean_mackrory has a point. Use a temporary string for the call to system/exec.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Hi

    system("cp name1 name2");

    You pass "cp name1 name2" to system() command! So, the command means
    "copy name1 as name1",it does not mean "copy the file whose name was defined by the user and contained in name1 as name2"!

    You have to declare a new char[] and then append it so that you construct a new array of char containing your bash commad. Maybe you want to have a look to sprintf()

    And one more suggestion, use fork()-exec() (actually one of exec family funcions) for calling another program inside of yours!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Everything you posted can already be found in this thread. Except
    And one more suggestion, use fork()-exec() (actually one of exec family funcions) for calling another program inside of yours!
    See the FAQ on that topic: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recieving pipes from bash
    By Drogin in forum Linux Programming
    Replies: 6
    Last Post: 04-12-2009, 04:09 PM
  2. Replies: 3
    Last Post: 04-10-2009, 12:57 AM
  3. bash scripting?
    By Draco in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2007, 06:15 AM
  4. Linux: Use C to call a bash script
    By harada in forum Linux Programming
    Replies: 9
    Last Post: 10-27-2006, 01:59 PM
  5. redirecting standard error in Bash
    By wozza in forum Linux Programming
    Replies: 3
    Last Post: 07-16-2002, 04:55 AM