Python implements Simple Shell__python

Source: Internet
Author: User

This paper is the experimental report of Experimental building course, course address:
https://www.shiyanlou.com/courses/647

no specific code involved

Before learning the Linux system programming time to learn to write a simple shell in C language, see today with Python implementation shell, I would like to take this opportunity to review the operating system shell-related knowledge.

What is a shell?

(1) Shell is a system software that is responsible for the interaction between the user and the operating system kernel, is the process of producing the process (through the Linux system call fork,exec), mainly responsible for interpreting the user's commands, and thus to achieve user control of the process.

(2) Shell is also a scripting language, with process control, variables, operations and so on, mainly applicable to the configuration and operation of Linux.

(3) The essence of the shell command is placed in the/bin directory of the executable program, pipeline and redirection operation is the terminal three standard file descriptor to operate the executables, and thus implemented (specifically).

(4) The signal is the core of event-driven programming and an important way to control the process through the shell.

(5) Has the environment variable, namely has the user, the version, the current directory and so on information, uses for the access control.

(6) A Shell forms a process session in which the process has a background of the foreground.

What is the shell that Python implements in this experiment?

(1) has the function of controlling the process.

(2) Support for simple built-in commands.

(3) scripting language features are not supported, piping and redirection operations are not supported.

learning points related to Python

(1) Python Os,sys module

The implementation process uses Python functions that encapsulate Linux system calls, such as:

Catalog-related

OS.GETCWD ()

Os.chdir ()

Path-related

Os.path.basename ()

Os.path.expanduser ()

Standard output File descriptor

Sys.stdout.write

Sys.stdout.flush ()

Getting environment variables

Os.getenv

(2) Python signal module

Ignore signal

Signal.signal (signal. SIGTSTP, Signal. Sig_ign)

(3) Python string handling

shelx.split//Decomposition Parameters

. startswith//the beginning of the judgment character

(4) subprocess

Equivalent to System call Fork,exec

Summary

(1) This experiment mainly manifests the python in the system programming ability, through Os,sys and so on module we can realize a good shell system software.

(2) Improve the direction: Add pipeline, redirect Operation,& background scheduling operation.

(3) For beginners to fully understand the students are more difficult, need to supplement the knowledge of system programming.

(4) Object-oriented, shell abstraction can be used to refactor the class

Related Posts:
"Unix/linux Programming Practice Course" Learning Notes: The eighth chapter writes the command interpreter SH
http://blog.csdn.net/yz764127031/article/details/61629928

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.