Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I write a C program to retrieve some information from the WebLogic Server. To do that I have to set upsome Environment Variables. For that I have a script ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    8

    how to execute script file unside C program

    Hi,
    I write a C program to retrieve some information from the WebLogic Server. To do that I have to set upsome Environment Variables. For that I have a script file to set Environment Variables. I want to execute the script file via my C program. Is it Possible?. Please help me.

  2. #2
    Just Joined!
    Join Date
    Feb 2008
    Posts
    50
    Yes, you can use "system" to execute your any file/shell command/shell script file.. etc. within your c program.
    e.g. (i)system("ls -al");
    (ii)system("./sample.sh");

    Thanks,
    Pankaj.

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by ypankaj View Post
    Yes, you can use "system" to execute your any file/shell command/shell script file.. etc. within your c program.
    e.g. (i)system("ls -al");
    (ii)system("./sample.sh");

    Thanks,
    Pankaj.
    This should not work because the variable is only available in the subshell.
    Execute your program with a script and export the variables before you execute your program.

    Regards

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Edit: the rest of this reply is completely wrong because I misread the question. Franklin52's answer is absolutely correct.

    This should not work because the variable is only available in the subshell.
    What variable? Or do you mean the output of the command?

    If you mean the output, there is a very simple way to gather the output in your C program after doing the system() call.

    First, do the system call thus:
    Code:
    system("whatever > output.txt");
    Then read the output file (which you can call something different, of course) in your C program.

    Two things to keep in mind:
    1. You should actually do something like this:
      Code:
      int result_code;
      
      /* ... */
      
      result_code=system("whatever > output.txt");
      and then examine the result code to make sure the program ran successfully. For more details, do this at the command line:
      Code:
      man 3 system
      If man pages are not installed on your system, google this:
      Code:
      man system linux
    2. There are faster ways to do this whole thing that are less makeshift but are more complex to code.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    If I'm not missing something, the OP want to set some environment variables in his C program with a script. In that case you can use the setenv function.

    Code:
    man setenv
    Regards

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Oops. Franklin52, your original answer was absolutely correct. My bad.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Feb 2008
    Posts
    50
    Yes, you are right.. "setenv" is right answer.

    Thanks,
    Pankaj

Posting Permissions

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