Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2003
    Posts
    13

    Unanswered: system command in C++ with arguments

    hi
    i am trying to run a shell script by calling it in a C++ program.
    I am using system command to do so.
    the shell script i am using needs two argument.
    in the C++ the two arguments are return values from two functions
    say arg1 arg2 (arg1 = 2000 , arg2 = 10)
    i am using the system command as follows

    system("sh ./shellscript arg1 arg2");

    but the shell script is not taking the value of arg1(i.e. 2000) instead its taking "arg1" as argument and giving improper result.
    Please guide me.
    regards
    sai koushal

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    If you mean that arg1 and arg2 are two variables inside your C++ program, then you need to format them into the command string that you pass to the system function. Keep in mind that this is a very crude example, just to get you started thinking, but:
    Code:
    char buffer[200];
    int arg1, arg2;
    arg1 = 2000;
    arg2 = 10;
    sprintf(buffer, "sh ./shellscript %i %i", arg1, arg2);
    system(buffer);
    I think this will get you started, but if not feel free to post again.

    -PatP

  3. #3
    Join Date
    Nov 2003
    Posts
    13
    Originally posted by Pat Phelan
    If you mean that arg1 and arg2 are two variables inside your C++ program, then you need to format them into the command string that you pass to the system function. Keep in mind that this is a very crude example, just to get you started thinking, but:
    Code:
    char buffer[200];
    int arg1, arg2;
    arg1 = 2000;
    arg2 = 10;
    sprintf(buffer, "sh ./shellscript %i %i", arg1, arg2);
    system(buffer);
    I think this will get you started, but if not feel free to post again.

    -PatP
    Thanks a lot....its working great

  4. #4
    Join Date
    Nov 2003
    Posts
    13
    Originally posted by Pat Phelan
    If you mean that arg1 and arg2 are two variables inside your C++ program, then you need to format them into the command string that you pass to the system function. Keep in mind that this is a very crude example, just to get you started thinking, but:
    Code:
    char buffer[200];
    int arg1, arg2;
    arg1 = 2000;
    arg2 = 10;
    sprintf(buffer, "sh ./shellscript %i %i", arg1, arg2);
    system(buffer);
    I think this will get you started, but if not feel free to post again.

    -PatP
    Thanks a lot....its working great

  5. #5
    Join Date
    Nov 2003
    Posts
    13
    Originally posted by Pat Phelan
    If you mean that arg1 and arg2 are two variables inside your C++ program, then you need to format them into the command string that you pass to the system function. Keep in mind that this is a very crude example, just to get you started thinking, but:
    Code:
    char buffer[200];
    int arg1, arg2;
    arg1 = 2000;
    arg2 = 10;
    sprintf(buffer, "sh ./shellscript %i %i", arg1, arg2);
    system(buffer);
    I think this will get you started, but if not feel free to post again.

    -PatP
    Thanks a lot....its working great

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    It is great that that is working for you. Let us know if you have any problems with it.

    -PatP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •