2013-10-06 34 views
0

我正在嘗試創建一個交互式shell程序,提示用戶輸入命令,解析命令,然後用子進程執行它。這是我有的代碼,但我不知道該PLEAE幫助後去哪裏!我正在嘗試創建一個交互式shell

Int shell(char *cmd_str){ 
int commandLength=0; 
cmd_t command; 
commandLength=make_cmd(cmd_str, command); 
cout<< commandLength<<endl; 
cout << command.argv[0]<< endl; 
if(execvp(command.argv[0], command.argv)==-1) 
//if the command it executed nothing runs after this line 
{ 
commandLength=-1; 

}else 
{ 
cout<<"work"<<endl; 
} 
cout<< commandLength<<endl; 
return commandLength; 


} 
+0

您可能會感到頭痛,但這裏有一個開始:http://rik0.altervista.org/snippets/csimpleshell.html只是複製代碼不會幫助您的教育。 –

+0

此外,您將此標記爲c,但您使用的是cout,它是C++重要:c *不是* C++ – Pankrates

回答

0

假設shell()被稱爲與fork()子進程中運行,你需要確保父進程正確地等待子進程終止。請參閱wait(2)系列功能。

此外,您需要檢索所述子進程的退出狀態(請參閱wait(2))。

您也可以嘗試實施流重定向。假設這是一個練習,我將離開額外的研究,如何實現這些東西給用戶:) - 看看dup(2)