Thread: multiple commands

  1. #1
    Registered User
    Join Date
    Dec 2004
    The Netherlands

    multiple commands

    Hello everyone,

    I am trying to make my program so that it can listen to multiple commands seperated by a character.

    Lets say I have a simple program like this:
    #include <stdio.h>
    int main(void)
    	char buf[128];
    	printf("Please enter a command: ");
    	if(!strcmp(buf, "command1")) puts("do something");
    	if(!strcmp(buf, "command2")) puts("do something else");
    	if(!strcmp(buf, "command3")) puts("....");
    	return 0;
    How could I make it so that it would accept an input like: command1 | command3
    I was thinking of using strchr, but how can I make it so that it compares to the buf, can anyone provide an example, that would be great.

    Best regards,

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Hmm, I wrote something like this awhile ago, but I doubt you want it to reach the same amount of depth.

    First of all, detecting the pipe character is as easy as you say: Use strchr() or whatever the function name is to find a character inside the string. Alternatively you could read character by character manually and find the pipe character that way. Regardless, if it's there, then you could break down the string into 2 different strings containing the two different commands.

    As a warning, however, the pipe character on the command line signifies more than just running two commands at once. It is used to start two separate processes and "pipe" the output of the first program into the input of the second program. Basically, stdout of process 1 is connected to stdin of process 2. It doesn't sound like this is what you want to do with your program, so if not, you can ignore this paragraph. If it is what you want to do (ie. mimic the way your terminal behaves), it can get somewhat complicated.


    Do not read strings with gets(). Use fgets() instead.
    Last edited by MacGyver; 03-23-2007 at 06:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with multiple pipes.
    By LightsOut06 in forum Linux Programming
    Replies: 3
    Last Post: 12-02-2010, 02:38 PM
  2. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Multiple System Commands
    By Denethor2000 in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2004, 08:35 PM