separating commands


 
Thread Tools Search this Thread
Top Forums Programming separating commands
# 1  
Old 09-12-2004
Question separating commands

hey there

well i have a small problem with my code. when for example :

" /bin/sleep 10 & ls -l mila > xyz " is entered, the program is supposed to separate the two commands 1) /bin/sleep 10 & and 2) ls -l mila > xyz. im not sure of how to achieve this. my current program stores both commands as one, eg: the same command is entered as above:

Command[0]:
com_pathname=/bin/sleep
argc=5
argv[0]=/bin/sleep
argv[1]=10
argv[2]=ls
argv[3]=-lt
argv[4]=mila
#######
redirect_in=NULL
redirect_out=pero
com_suffix=

the correct version would be:
Command[0]:
com_pathname=/bin/sleep
argc=2
argv[0]=/bin/sleep
argv[1]=10
#######
redirect_in=NULL
redirect_out=pero
com_suffix= &

Command[1]:
com_pathname=ls
argc=3
argv[0]=ls
argv[1]=-lt
argv[2]=mila
#######
redirect_in=NULL
redirect_out=pero
com_suffix=

---

when only one command is entered, the program stores correctly.
here's the code so if anyone could help, would appreciate it:

Code:
 
//---------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*function prototypes*/
void student_details();  
void option_exit();
void printComStruct(struct command *com);

//---------------------------------------------------------------------------------------------------

struct command { 
    
	char *com_pathname;		// what is the path name of the command

	int argc;               // the number of arguments to the command
                            // including the command itself

	char *argv[40];			// pointers to strings, each string
                            // is an argument for the command, including
                            // argument "0". The last pointer should
                            // be set to NULL. 

	char *redirect_in;       // if this is not NULL, then the standard input
                            // is redirected to the given file name

    char *redirect_out;      // if this is not NULL, then the standard output
                            // is redirected to the given file name

    char *com_suffix;        // ' ' - no command suffix (last command);
                            // '&' - the command is followed by '&'
                            // ';' - the command is followed by ';';
                            // '|' - the command is followed by '|'.
};

//---------------------------------------------------------------------------------------------------

int main() {

	/*declaration*/
	char *guess = "exit";
	char line[256]; 
	char *break_set = "\n\f\v\\\"~!@#$%^*()_=+'' [] {}:?,";    

    int nCommands = 0;          // number of commands in a command line
	int state = 0;				// if a file has been redirected either way
								// this changes to 1
	int i = 0;
	int c;

	struct command com[10]; // storage for each command

	com->argc = 0;
	
    student_details();  /*present information about the user*/

             /*introduction of what the program is supposed to do*/
   	printf("          Hello and welcome to my program\n");
	printf("\n");
	printf("  This program will repeatedly take one line of input  \n");
	printf("  from the console until the input line is 'exit'. For  \n");
	printf("  each input line, break it down into tokens. The input \n");
	printf("  line should result in a 'dump' of the raw contents of \n");
	printf("  several structures, depending on how many commands \n");
	printf("  were given.\n");
	printf("\n");

	printf(" Mila's Shell> ");
	
	// increment number of commands by 1 because
	// it is start of the program
	nCommands++;
	
	while (gets (line)) {		 

       char *tokp; 
	   char *sp = line; 

       while ((tokp = strtok(sp,break_set)) != NULL) {
		

		   if (strlen(tokp)==1) { /* if the length of the character is 1 */

			  switch (*tokp) { /* switch on the first char */
          

			  case '>': /* output is redirected */
				   
				   state = 1;
				   break;
		
			   case '<': /* output is redirected */

				   state = 2;
				   break;

               case '|': /*  directs the standard output of one Unix command into the 
							 standard input of another Unix command. */
   
				   com[nCommands-1].com_suffix = tokp;
				   break;
		
			   case '&': /* a command shall run in the background */
				   
				   // save in suffix
				   com[nCommands-1].com_suffix = tokp;
				   break;

			   case ';': /* end of command */
				   
				   // save in suffix
				   com[nCommands-1].com_suffix = tokp;
				   break;

			  } // end switch 

		   } // end if 

		   // ---------------------

		     else {

				if (state == 1) {

				    com[nCommands-1].redirect_out = tokp;	
				}

				if (state == 2) {

					com[nCommands-1].redirect_in = tokp;
				}
			
				  else if (state == 0) {

					// save token as argument for command
					// argv = arguments for commands
					// argc = number of arguments to the command;
					com->argc++;
					com->argv[com->argc -1] = tokp;
		
					/* 
					* everytime the program is started the first argument of
					* the input line is stored as the first command
					*/

					if (com->argc == 1) {

						com[0].com_pathname = tokp;
					}

				// -------------------------
			
					com[nCommands-1].redirect_in = NULL;
					com[nCommands-1].redirect_out = NULL;
					com[nCommands-1].com_suffix = NULL;

			 // ---------------------------

					sp = NULL; // continue in this string 
				 } // end else if

			} // end else

       } // end while 

	   /*
		* checks if the input line equals 'exit'. if it does	 
		* the function option_exit is called and the 
		* program terminated. if it does not, another
		* prompt is displayed to allow the user to enter
		* more commands
	   */
	   
	   if(strcmp(line,guess) == 0) {

          option_exit();
       }

       else {

		  com->argv[com->argc] = NULL;

		  // print command structures
		  printf("\n");
		  fprintf(stderr, "*** ICT310 construct dump begin ***\n");

		  for (i = 0; i < nCommands; ++i) {

			fprintf(stderr, " Command[%d]: \n", i); 
			printComStruct(&com[i]);

			for (c = 0; c < com->argc; c++) {

			  com->argv[c] = NULL;
		  }
	  
		  com->argc = 0;
		  nCommands = 0;
		  state = 0;
            
		  }
	
		  fprintf(stderr, "*** ICT310 construct dump end ***\n");

		  for (c = 0; c < com->argc; c++) {

			  com->argv[c] = NULL;
		  }
	  
		  com->argc = 0;
		  nCommands = 0;
		  state = 0;

		  printf("\n");
	      printf("\n Mila's Shell> ");
		  nCommands++;

		  // everytime a user is presented the prompt, increment
		  // the number of comamnds by 1;
       } // end else

	} // end while

	return 0;	

} // end function main()

//---------------------------------------------------------------------------------------------------

void student_details() {  /*function definition*/

	/*student's details*/
	printf("\n");
	printf("\n");
	printf(" Student's Name:	Milos Zdravic\n");
	printf("\n");

} /*end of function*/

//---------------------------------------------------------------------------------------------------

void option_exit() {     /* exit option*/

	printf("\n");
	printf("\n");
	printf("   Thanks for using my program. Wasn't too bad I hope!!!");
   	fflush(stdin);
    printf("\n");
	exit(0);

} /*end of function*/

//---------------------------------------------------------------------------------------------------

void printComStruct(struct command *com) {  /* print function */
 
    int i;
	
	fprintf(stderr,"com_pathname=%s\n", com->com_pathname); 
    fprintf(stderr,"argc=%d\n", com->argc);	
 
	for(i=0; com->argv[i] != NULL; i++) {

       fprintf(stderr,"argv[%d]=%s\n", i, com->argv[i]); 
	}
       
	fprintf(stderr,"#######\n"); 
        
	if (com->redirect_in == NULL)
            fprintf(stderr,"redirect_in=NULL\n"); 
         
	    else
			fprintf(stderr,"redirect_in=%s\n", com->redirect_in);
         
	if (com->redirect_out == NULL)
            fprintf(stderr,"redirect_out=NULL\n"); 
         
	    else
			fprintf(stderr,"redirect_out=%s\n", com->redirect_out); 
         
	fprintf(stderr,"com_suffix=%c\n\n", com->com_suffix); 

} /*end of function*/

//---------------------------------------------------------------------------------------------------

# 2  
Old 09-13-2004
Shell syntax would be
Code:
" /bin/sleep 10 && ls -l mila > xyz "

Note the &&

You can also separate commands with a ;

You may have other problems, if it still has issues try

Code:
" /bin/sleep 10 && ls -l mila > xyz 2>error.log "

read error log for problems. Remove the '2>error.log' for production.
# 3  
Old 09-13-2004
Re: separating commands

Quote:
Originally posted by mile1982
Code:
 void student_details() {  /*function definition*/

	/*student's details*/
	printf("\n");
	printf("\n");
	printf(" Student's Name:	Milos Zdravic\n");
	printf("\n");

} /*end of function*/

From our rules:

(7) Do not post classroom or homework problems.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separating words in a line

Hi I have the following file # cat red it.d-state = 50988 41498 45 0 0 0 it.age_buffer= 1500 it.dir = 1 I need to grep lines that are present after "=" But when I do so, few lines has more than one value and some has one How to parse the line I have used the follwing... (6 Replies)
Discussion started by: Priya Amaresh
6 Replies

2. Shell Programming and Scripting

Help in separating a multilingual file

Hello, I have a text file running into around 100 thousand+ lines which has the following rigid structure: Each field is separated by a comma. Some examples are given below: 23,Chinttaman Pagare,चिंतमण पगारे 24, Chinttaman Pateel,चिंतामण पाटल 25, Chinttaman Rout,चिंतामण राऊत 26,... (3 Replies)
Discussion started by: gimley
3 Replies

3. Shell Programming and Scripting

Adding and then separating the output

Hi I have this output /vol/vol0 4GB /vol/nonprod00 682GB /vol/prod00 3GB /vol/nasp_archive 201GB /vol/nasp_iface 92GB /vol/nasp_bsi 0GB /vol/nasp_vertex 0GB /vol/nasp_sapmnt_mp2 1GB /vol/nasp_sapmnt_prd 52GB /vol/nasp_sapmnt_srp 1GB /vol/nasp_smd 1GB /vol/nasp_ccms 0GB... (8 Replies)
Discussion started by: bombcan
8 Replies

4. Shell Programming and Scripting

Need help separating a file

Hi all, I have a single text file, Contig3.fasta, that looks like this: >NAME1 ACCTGGTA >NAME2 GGTTGGACA >NAME3 ATTTTGGGCCAnd It has about 100 items like this in it. What I would like to do is copy each item into 100 different text files, and have them named a certain way Output... (4 Replies)
Discussion started by: repiv
4 Replies

5. Shell Programming and Scripting

separating folders

I have folder like main. inside main folder there are subfolders & files like main1 main2 main3, file1, file2, file3. I want folders main1 & main2, file1, file2 from main folder. copy them into new folder. Please suggest me how to do it. I am new to shell programming (2 Replies)
Discussion started by: ypremcha
2 Replies

6. Shell Programming and Scripting

Separating fields

Hi, I have a text file in following format: 2.45 5.67 6.43 I have to cut the values before decimal and store them in a file. So the output file should look like: 2 5 6 . . and so on... Can someone suggest me a sed/awk command for doing this? (2 Replies)
Discussion started by: sajal.bhatia
2 Replies

7. Shell Programming and Scripting

Help with separating syslog messages.

Hello Guys... I am bit new to shell scripting and was looking for help !! I have got syslog data on a linux server recording log messages from a device. I need to seperate the data from log to file so that I can push it excell and get a report from that. Log is in the format below "... (2 Replies)
Discussion started by: raj_26apr
2 Replies

8. Shell Programming and Scripting

separating filename and extension

Hi (warning: newbie question), I am writing a script to run a series of tests on a program, which involves a line: for file in `ls test_suite/*.args` but later I want to send the output to file.out. But I need to separate the filename and extension somehow...Also $file contains... (2 Replies)
Discussion started by: lucaspewkas
2 Replies

9. Programming

Separating commands/programs with ;

Hi i have encountered a problem and i have tried many different things but my brain just has some limitations lol well anyways i was trying to make this program work down below so i can process multiple commands just by separating them with ;. I would apeciate if someone could just make it work kuz... (2 Replies)
Discussion started by: dush_19
2 Replies

10. Shell Programming and Scripting

separating fields

Hi, i have a file as follows: jonathan:bonus1,bonus2 gerald:bonus1 patrick:bonus1,bonus2 My desired output is jonathan:bonus1 jonathan:bonus2 gerald:bonus1 patrick:bonus1 patrick:bonus2 my current code is cat $F | awk -F"" how should i continue the code? Can i do something... (5 Replies)
Discussion started by: new2ss
5 Replies
Login or Register to Ask a Question