I'm currently working on writing a shell program in C. I have most of the basics down, but I can't seem to figure out how to get the ";" to work properly, that is, to make it so when I type something like "command1 ; command2" when the shell is running, it executes the first command AND THEN the second command. Right now, it basically has the same functionality as the "|" command. Here is the code for my ";" command section:
char* cmd = line;
char* also = strchr(cmd, ';'); /* Finds first ';'*/
char* next = strchr(cmd, '|'); /* Find first '|' */
while (also != NULL){
/* 'also' points to ';' */
*also = '\0';
input = go(cmd, input, first, 0);
cmd = also + 1;
also = strchr(cmd, ';'); /* Find next ';' */
first = 0;
}
`
Anyone have any tips on how I can get this working properly? Thanks in advance!
The issue with your code is that when you input command1 ; command2
command1 ; command2
you'll go execute command1
, but since you're looking for another ;
command2
will never be executed, since also will be set at null
. An input with command1 ; command2 ;
command1 ; command2 ;
will be executed though.
What you want to do is looking up for either a ;
or a '\\n'
if thats from the standard input, or \\n
or #EOF
if you're reading from a file. So you need to make changes at this very line :
also = strchr(cmd, ';');
Good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.