Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

2nd edit

Some clarifying...

I wrote a C program that asks for input (in this case, using fgets()). When I compile it into a C executable, I notice this behavior:

If I run the executable from the terminal like: ~$ ./program_name obviously the terminal session continues after the process is completed. I am greeted with a blank command line, as usual: ~$

However, if I run the executable by double-clicking the C executable file, a terminal comes up and I am able to input data, as the program asks. After the data is inputted the process ends, the "terminal" says [Process completed] and the terminal is apparently unusable.

When I run a C executable in OSX that allows terminal input (via fgets(), for instance), after the information is entered the message [Process completed] appears and the terminal seems unusable. This is inconvenient for my purposes.

Questions: Let's assume I'm running it without a terminal open. So i'm double clicking the program.

  • Is there a way to remain in the terminal after executing the C program via a command line input?

    • My program asks for input, so I can give it input that can echo in the shell... is there any way/command to remain in the terminal? Spawning a new terminal is an option, but it is far less desirable.
  • Is there a way to remain in the terminal by altering my C program?

Additional question: Is there a terminal command to spawn a new terminal window?

All ideas are welcome...! I have some, but nothing satisfactory, yet.

share|improve this question
    
What o you mean by "executing the C program via a command line input"? The second case, launching it from terminal? Are you the developer of this program, or was it written by a third party? –  Daniel Beck Jan 24 '13 at 6:54
    
@DanielBeck I mean: ~$ ./program_name A little bit of both: I read about it in a book, implemented it, and encountered this not discussed behavior. –  d0rmLife Jan 24 '13 at 6:58

1 Answer 1

[Process completed] is what Terminal writes to the terminal display when your terminal session has ended, i.e., there are no more processes associated with the tty. This indicates that your program exited and you didn’t start the terminal session from an interactive shell.

There are several ways to execute a command in Terminal without manually interacting with a shell, including:

  1. Choose the Shell > New Command menu item and enter a command. Leave the Run command inside a shell checkbox unselected. Your program will be executed directly without a shell. If you open the Inspector window and look at the Info pane, you’ll see the list of processes only lists “login” and your command, but no shell.

  2. Open a *.command file from Finder. Terminal will start a shell and tell it to execute the command file and then exit. You’ll see a shell prompt followed by a command that looks like: /path/to/your/file.command ; exit;. Again, if you look at the Inspector, the list of processes will include “login”, your shell, and your command executable. (This is also what happens if you select Run command inside a shell in the New Command panel.)

It sounds like you are using #2. There is no way to alter your program to have the shell remain interactive after your program exits; however, there are a couple of indirect ways to have Terminal run a command and then leave you with an interactive shell:

  1. Create a settings profile that runs your command in an interactive shell:

    1. In Preferences > Settings duplicate the default profile using the action (gear) menu at the bottom of the profiles list.
    2. Go to the Shell pane and select Run command:.
    3. Enter the pathname to your command file.
    4. Select Run inside shell.
    5. Export the settings profile to a *.terminal file, either with the Export command in the action menu, or by dragging it from the profiles list to Finder.

    Then, whenever you want to create a new shell that starts by running your program, open the *.terminal file. You can also just open a new terminal with this profile from inside Terminal, either using the Shell > New Window [or Tab] menu, or by double-clicking the profile in the profile list.

  2. Write an AppleScript that directs Terminal to run your command, as if you’d entered it into the command line. You can then either store the script in the system-wide Script menu and invoke it from there, or you can save the script as an application that you can run. The script would look like this, for example:

    tell application "Terminal"
        activate
        do script "/path/to/your/file.command"
    end tell
    
share|improve this answer
    
I clarified my question: I'm echoing commands, so I'm not sure about *.command. The second 2 is interesting. I have never used AppleScript, but if the command you provided is syntactically correct it seems pretty straightforward. –  d0rmLife Jan 24 '13 at 19:01
    
Also, can the AppleScript be used to invoke a C program rather than a *.command file? –  d0rmLife Jan 24 '13 at 19:04
    
If you use AppleScript (or, actually, any language that can send Apple Events) to send a "do script" event to Terminal, Terminal just sends the text to the tty as if you had typed it. So, you can have it execute any command you can enter, or even enter text that isn't a command (e.g., to enter text for an editor, or data for some other sort of program). –  Chris Page Jan 28 '13 at 9:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.