Thread: Running 'exec' twice in one UNIX shell script

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Running 'exec' twice in one UNIX shell script

    I have two programs that need to be executed one right after another. I'm trying to write a little shell script with two calls to exec, but it stops after the first program completes. Here's what I've got:

    #! /usr/bin/sh
    exec prog1 $1 $2
    exec prog2 $2

    Do I need to put these in a loop or something? I'm really new to shell programming so I don't really have any idea what to do to fix this. Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    >I don't really know shell scripting either, but what does ``exec'' do?

    I believe exec is what you use to run an executable.

    Code:
    #!/bin/sh
    
    prog1 $1 $2; prog2 $2
    That did the trick. Apparently when 'exec' finishes it kills the shell, therefore not allowing any other program to run. (I tried typing 'exec prog1' in the terminal window and the terminal closed on me). Thanks alot.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    exec is like exec in C, it replaces the running shell script with that executable. So your second exec will never be run because the call no longer exists.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-13-2009, 09:55 AM
  2. using tar command in a shell script
    By rohan_ak1 in forum Linux Programming
    Replies: 1
    Last Post: 05-10-2008, 07:03 AM
  3. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  4. how to compile & run c programs in unix?
    By Unregistere in forum C Programming
    Replies: 2
    Last Post: 10-09-2002, 10:53 PM
  5. need to see if C program is already running in UNIX
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 12:17 PM