Self-compiled simple shell command interpreter requirements and code

Source: Internet
Author: User
Compile simple shell command interpreter requirements and code-Linux general technology-Linux technology and application information. The following is a detailed description. Requirements:
Step 2: Write the 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:
The command interpreter is an endless loop first.
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 content to be completed is as follows:
The command interpreter is an endless loop first.
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 separate the commands and parameters separated by Spaces
Put them 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.
Create a sub-process and call exec to execute the command.
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:
Based on step 1, after the command line input is analyzed, check whether the input command arg [0] is "exit" or "cd". If yes, run this command as an internal command before executing the create sub-process to execute this command.
To implement the exit command, you only need to print a sentence "Bye bye !", Then, release the previously allocated memory space and exit.
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:
Introduce the header file and put the public variables and functions in the header file. Be sure to prevent the header file from being repeatedly contained.
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: import and not to the environment variable configuration file mysh_profile, read the environment variable, and determine whether the file exists. If yes, run the command; otherwise, print "comm found ".

The content to be completed is as follows:
Create an environment variable configuration file with the following PATH environment variables: PATH =/bin:/sbin:/usr/bin:/usr/sbin
Create the init_environ () function to read the environment variables to the array.
Create the is_founded () function to find the file and check whether the file exists.

Step 2: implement the redirection function.

The content to be completed is as follows:
Implement the redirect () function in the redirect. c file.

Step 2: implement the MPs queue Function

The content to be completed is as follows:
Implement the my_pipe () function in the pipe. c file.

Step 2: implement the history command history

The content to be completed is as follows:
In the header file mysh. h, add the Data Type of the cyclic array that represents the history and define the corresponding variables.
Implement the add_history () and history_cmd () functions in history. c ().
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.
Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.