Help needed, new in UNIX programming

Help needed, new in UNIX programming

Post by Pawel Szymansk » Thu, 07 Oct 1999 04:00:00



Hello everyone!

I'm quite new in UNIX programming and I'm trying to create a program
that will do something like that:
- there are some script files that set some environment variables stored
on the disk and there are also some programs that do a short work, but
using the variables set by these scripts.

- the program, first,  needs to invoke one of the script files to set
the environment
- then after some computation it has to launch other program in the
environment set by the script file invoked at the begining.
- launch another program, and so on...

Is it possible to do something like this using C programming (not
shell)?

Using popen/pclose I can only execute one command at a time, using execl
I also do not see the solution.
Is there any other way?

Please help.

Kind regards,
Pawel.

--
Pawel, Szymanski                       Data Development Sig/DA
Networks Application Development       phone +48 52 349 1394

 
 
 

Help needed, new in UNIX programming

Post by Chine chanté Bl » Thu, 07 Oct 1999 04:00:00


/ I'm quite new in UNIX programming and I'm trying to create a program
/ that will do something like that:
/ - there are some script files that set some environment variables stored
/ on the disk and there are also some programs that do a short work, but
/ using the variables set by these scripts.

- In sh or ksh, unless you export variables, they won't be visible to any
programs run in the script. I think csh has some similar distinction.

- When you execute a script and it completes, all of its variables,
exported or no, are vapourised.

I don't think there's any easy way of exec one and then another and
then.... Instead I would concatenate the exec files and give them to the
shell to evaluate.

    cat a.script b.script ... z.script | sh

You should be able to construct this command and give it system(), or
possibly just popen(getenviron("SHELL"),"w") and then open and copy each
script file into the pipe yourself.

(Could...should...I've never actually done this. Your mileage may varied.
Prizes may be substituted with objects of equivalent worth at sponsor's
whim.)

--
Collective Against Consensual Sanity
v0.1 is sort of up.   http://www.angelfire.com/ca3/cacs
For a free CACS T-shirt send $42 shipping and handling.
pretty?     http://www.geocities.com/SoHo/Studios/5079/

 
 
 

Help needed, new in UNIX programming

Post by v.. » Thu, 07 Oct 1999 04:00:00


NOTE: Reply parts are posted inside original message.



Quote:> Hello everyone!

> I'm quite new in UNIX programming and I'm trying to create a program
> that will do something like that:
> - there are some script files that set some environment variables
stored
> on the disk and there are also some programs that do a short work, but
> using the variables set by these scripts.

> - the program, first,  needs to invoke one of the script files to set
> the environment

Try 'status = system(". /path/script_name")'
The '.' is used to "source" the shell script into the current process.
I use this when I make changes to my .profile and want the changes to
show up in the current session.  You may have to make modifications to
the scripts to get it to work.  Or, you can read the script and
use "putenv" (stdlib.h) to change the environment variables.

Quote:> - then after some computation it has to launch other program in the
> environment set by the script file invoked at the begining.
> - launch another program, and so on...

> Is it possible to do something like this using C programming (not
> shell)?

Most anything is possible, it may just be difficult.

Quote:> Using popen/pclose I can only execute one command at a time, using
execl
> I also do not see the solution.
> Is there any other way?

Multiple commands can be executed at the same (actually similar) time
by forking, then using execl.

example (in pseudo code):

int runCommand(const char *commmand_str)
{
  int forkpid, status;

  forkpid = fork();

  if (forkpid == 0)   /* This is the child */
  {
      status = execl(command_str,...);
      exit(status);                     /* Note: you must exit here, */
                                        /* or you will have multiple */
                                        /* mainlines running...      */
  }
  else if (forkpid > 0)  /* Parent, no error */
  {
      status = 0;
  }
  else   /* Parent, with error (forkpid == -1)
  {
      status = -1;
  }
  return(status);

Quote:}

int main(void)
{
   int proc_count = 0, child_status = 0, status = 0, dead_pid = 0;
   int exit_flag = 0;

   /* Fire off the commands */
   status = runCommand("command1");
   if (!status)
       proc_count++;
   status = runCommand("command2");
   if (!status)
       proc_count++
   status = runCommand("command3");
   if (!status)
       proc_count++;

   /* Wait till the commands complete */
   while (proc_count && !exit_flag)
   {
       dead_pid = waitpid(-1,
                          &child_status,
                          WNOHANG);
       if (dead_pid < 0)
       {
           /* wait pid error - exit the loop */
           exit_flag = 1;
       }
       else if (dead_pid > 0)
       {
           proc_count--;
           /* You can check the process exit status here - its in the */
           /* child_status variable.  man waitpid for more info */
       }
       else  /* dead_pid == 0, no processes died */
       {
           sleep(3);   /* give them time to die */
       }
   }

Quote:}

Oh, another way to wait for the children is to enable the SIGCHILD
signal, and write a handler.

> Please help.

> Kind regards,
> Pawel.

> --
> Pawel, Szymanski                       Data Development Sig/DA
> Networks Application Development       phone +48 52 349 1394


Welcome to UNIX, which despite its name does have children....

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Help needed, new in UNIX programming

Post by Owen M. Astle » Thu, 07 Oct 1999 04:00:00





> > Hello everyone!

> > I'm quite new in UNIX programming and I'm trying to create a program
> > that will do something like that:
> > - there are some script files that set some environment variables
> > stored
> > on the disk and there are also some programs that do a short work, but
> > using the variables set by these scripts.

> > - the program, first,  needs to invoke one of the script files to set
> > the environment

> Try 'status = system(". /path/script_name")'
> The '.' is used to "source" the shell script into the current process.
> I use this when I make changes to my .profile and want the changes to
> show up in the current session.  You may have to make modifications to
> the scripts to get it to work.  Or, you can read the script and
> use "putenv" (stdlib.h) to change the environment variables.

The first suggestion of using system() won't work.  system will
run sh to execute a command, so the new sh will get the envionment
variables set in /path/script_name, but will then exit, and the calling
envionment still won't have the variables set.

For example, this would be like doing (purely in shell form)

$                      # line 1
$ sh;                  # start new shell
$ . /path/script_name
$ exit;                # exit shell
$                      # back in original shell, which is equivalent to
                       # after the system() call finishes,
                       # and equivalent to the state on line 1

The bit about using putenv() would work though.

Owen

 
 
 

Help needed, new in UNIX programming

Post by Floyd Davids » Thu, 07 Oct 1999 04:00:00




>> I'm quite new in UNIX programming and I'm trying to create a program
>> that will do something like that:
>> - there are some script files that set some environment
>> variables stored on the disk and there are also some programs
>> that do a short work, but using the variables set by these
>> scripts.

>> - the program, first, needs to invoke one of the script files
>> to set the environment

>Try 'status = system(". /path/script_name")'
>The '.' is used to "source" the shell script into the current process.

Won't work.  The "current process" is a shell spawned by the system()
function, which indeed will see the changes to the environment in
the files sourced, and then will exit and that environment will
evaporate.

Quote:>I use this when I make changes to my .profile and want the changes to
>show up in the current session.  You may have to make modifications to

But you are doing that from a shell commmand line, and that
shell (your login shell most likely, though it might also be a
xterm or whatever) stays around until you purposely exit it.
But the system() function is identical to a command line
doing:

    sh -c '. /path/script_name'

which likewise will set the environment in the sh shell, and then
evaporate.

Quote:>the scripts to get it to work.  Or, you can read the script and
>use "putenv" (stdlib.h) to change the environment variables.

That is exactly the way to accomplish it.

Quote:>> - then after some computation it has to launch other program in the
>> environment set by the script file invoked at the begining.
>> - launch another program, and so on...

>> Is it possible to do something like this using C programming (not
>> shell)?

>Most anything is possible, it may just be difficult.

>> Using popen/pclose I can only execute one command at a time,

Actually you can popen several commands at once.  The problem is
that if you do any type of a read on a descriptor that can block
if there is no data available, it might just happen that no data
is available...  and everything comes to a screeching halt.

Look up the select() function which will allow the program to
read only from descriptors which will not block on a read.

Quote:>> using execl I also do not see the solution.  Is there any
>> other way?

Using the exec family of functions means you have to do your
own fork() and take care of a number of other details.  If your
needs are different than the way popen() takes care of those
details, then you will want to role your own.

  Floyd

--

Ukpeagvik (Barrow, Alaska)

 
 
 

1. NEED HELP WITH PROGRAMMING POP3 MAIL PROGRAM TO RETRIEVE MAIL ON UNIX-TO-UNIX

I'm a student at the University of Mississippi and my next programming
assignment is to write a C program on a unix box that will retrieve
POP3 mail from another unix machine.  Is there a really easy way of
doing this?  And if so, some sample code or description of "how to"
would be much appreciated.  Thanx.


2. I don't get cdrecord working.

3. I am new to Unix and Unix Programming HELP!

4. Best Motif for Linux?

5. New HOWTO....HOWTO-Hose-RedHat-Installation (help!)

6. Help installing PAS under Linux

7. Need HELP to Log User Log-ins form the internet

8. 100x30 mode problem.

9. New linux/UNIX programs collection - new version of RXVT and more

10. Need Help on setting up getty for dial-ins

11. I am new to UNIX/C programming: HELP!

12. Q: A UNIX COMMUNICATION PROGRAMMING QUESTION. NEED HELP!

13. Need help in programming a UNIX RTL