Write a simple shell command parser environment: RedHat9.0 kernel 2.4.20vi file editor 6.1.320gcc3.2.2-5 implementation steps: Step 5: write a simplest shell command interpreter, this program is taken from APUE example 1-5. the command cannot contain parameters. The content to be completed is as follows... compile a simple shell command parser environment: RedHat 9.0 kernel 2.4.20 vi file editor 6.1.320 gcc 3.2.2-5 implementation steps: Step 5: write a simplest shell command interpreter, this program is taken from APUE example 1-5. the command cannot contain parameters. The command interpreter is an endless loop. Print a command prompt. Put the command line input in the array without the command with parameters. Getc (), fgets (), and scanf. If fgets () is used, the obtained string includes the last line break. Therefore, remove "/n" at the end of the command string and change it to "/0 ". Create a sub-process and call exec to execute the command. The parent process calls waitpid () to wait for the child process to exit, and then enters the next loop. Step 2: write a shell command interpreter to enable processing of commands with parameters. The command interpreter is an endless loop. Print a command prompt that contains the current path information. Obtain the command line input. this program saves the command line input in the address pointed to by a character pointer. Analyze the command line and place the commands and parameters separated by spaces in the arg [] character pointer array. The command line string obtained here is saved at the address pointed to by input. To separate the commands and parameters in this string, a temporary array tmp is required (this program is to reuse the previously used buf array ), point input to the commands and parameters in the command line and save them in arg [0], arg [1], and so on. 4. create a sub-process and call exec to execute the command. 5. the parent process (that is, the shell command interpreter) determines whether to call waitpid () based on whether the command runs on the foreground or background (). Then enter the next cycle. Step 2: Add the internal commands cd and exit. The content to be completed is as follows: 1. based on step 1, after the command line input is analyzed, check whether the input command arg [0] is "exit" or "cd". if yes, execute this command as an internal command before executing the create sub-process to execute this command. 2. to implement the exit command, just print a sentence "Bye bye !", Then, release the previously allocated memory space and exit. 3. the implementation of the cd command uses the chdir (arg [1]) function to change the current working directory. Step 2: Program implementation is put into several files and the concept of header files is introduced. Introduce the redirection and pipeline functions, but you do not need to implement these two functions. The content to be completed is as follows: 1. introduce the header file and put public variables and functions in the header file. Be sure to prevent the header file from being repeatedly included. 2. handle situations where user-entered command lines contain redirection and pipeline symbols. Use the redirect () and my_pipe () functions to process redirection and pipelines. the implementation of these two functions is placed in a separate file, so you do not need to implement these two functions. Step 2: Introduce the environment variable configuration file mysh_profile, read the environment variable, and determine whether the file exists. If yes, run the command; otherwise, print "command not found ". The content to be completed is as follows: 1. create an environment variable configuration file with the following PATH environment variables: PATH =/bin:/sbin:/usr/bin:/usr/sbin 2. create the init_environ () function to read the environment variables to the array. 3. create the function is_founded () to find the file and check whether the file exists. Step 2: implement the redirection function. The following describes how to implement the redirect () function in the redirect. c file. Step 2: implement the pipeline function as follows: implement the my_pipe () function in the pipe. c file. Step 1: implement the history Command history to complete the following: 1. add the data type of the loop array that represents the history in the header file mysh. h and define the corresponding variables. 2. implement the add_history () and history_cmd () functions in history. c (). 3. in main. c, add the command line content to the command history loop array before pipeline, redirection, internal commands, and common commands are executed. Step 2: implement the background job queue and add the internal commands jobs, bg, and fg. Function completed: Step 1: 1. after each command is complete, print the prompt again. test: ls cat command 1st step: parameter 1. the current path is displayed in the prompt. 2. test the command "ls" and "ls ". <空格> "," <空格> Ls "," <空格> Ls <空格> "Ls-a-l"," <空格> Ls <多个空格> - <多个空格> -L "step 2nd: test" cd... "," cd/home "," cd-"," cd ~ "Wait for command 4th step: environment variable 1. copy the/bin/ls file to the/home directory, rename/bin/ls to/bin/ls-bk, and test the ls command 2. add the "/home" directory to the environment variable file mysh_profile and test the ls command step 5th: redirect 1. test the "ls> test", "ls (space)> (space) test", "ls (space)> test" Command 2. test "ls> test", "ls (space)> (space) test", "ls (space)> test" command 3. test "cat Test Test1 > Test "generation step 6th: MPs queue 1. test the "ls (space) | (space) more" command 7th step: History 1. extract commands from history using the up/down key for testing step 8th: job management 1. test jobs, bg, fg, "Ctrl + c", "Ctrl + z" command Summary: The main knowledge involved in this project: process control, process communication. Of course, the basics of C language are the most important. The basic functions of the project have been implemented, but there are some issues to be resolved: 1. after History commands are called up, the corresponding commands cannot be executed after they are added or removed. when the 2.jobs linked list is created, no judgment conditions are added, duplicate contents are saved in the linked list when the background commands are executed.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service