簡體   English   中英

為一個簡單的 shell 程序解析多個命令

[英]Parsing multiple commands for a simple shell program

我正在嘗試編寫一個簡單的 shell 程序,該程序能夠解析由分號分隔的多個命令。 例如 ls;pwd 會將 function 作為兩個單獨的命令。 我已經找到了單個命令,但無法解析多個命令。 由於 windows 不允許 fork() 我如何解析多個命令。 我可以使用 strchr() 檢查分號,但不知道如何構建一個可以解析多個命令的 function。 任何幫助,將不勝感激。

int main (int argc, char* argv[]){

char line[MAX];
char *newline;
char *input[50];
char  newinput[MAX];
char *exitString="exit";
char *open ="ls";
char *executepwd ="pwd";
int i=0;

while (1){

getcwd(current_directory, sizeof(current_directory));

printf("$->");

fgets(line,MAX,stdin);

if (strchr(line,';')){

//I do not know how to parse multiple commands without fork()   

    printf("There are a lot of commands");

}


else{
//I could parse single commands but not multiple

if (strstr(input[0],exitString)){
    printf("Exiting the program\n");
    exit(0);
    printf("\n");

}

else if 
    (strstr(input[0],open)){
    ls();
    printf("\n");
      }
    }
}

使用 getopt 解析並獲取程序的輸入參數

https://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM