Linux OS Programming Shell Quiz

Source: Internet
Author: User

A few days ago, in the process of discussing a statistic on the number of scripts being executed on Cu, I found myself ashamed of how little I knew about how the shell executed the command ... During the period get Waker brother's advice, thank you here! His remarks are basically correct, except that they are not very accurate in individual places. These days took time to find some data research, and learned a lot! Here I have a little experience in the form of questions and answers for your reference. Little brother Talents, the wrong place must be a lot, welcome everyone to shoot bricks, correct me!

Q1:shell How do I perform the "simple" command?

A: The simple commands here are the same as in the Bash Reference manual, typically: the name of the command plus its arguments. There are three of different simple commands:

1. Built-in command (BUILTIN)

is built within the shell interpreter, with the shell executing directly without the need to derive a new process. There are internal commands that can be used to change the current shell environment, such as:

cd /path
var=value
read var
export var
...

2. External command ("external command" or "Disk command")

Binary executables, which require disk loading into memory execution. Derives a new process, the Shell interpreter invokes a copy of fork itself, then executes the external command with the EXEC series function, and the external command replaces the previously fork child shell.

3.shell Scripts (script)

The shell interpreter fork+exec execute the script command, in which the kernel checks the first line of the script (e.g. #!/bin/sh), finds the interpreter used to execute the script, and then loads the interpreter to interpret the script execution. There may be many kinds of interpreters, various shells (Bourne Shell,korn shell CSHELL,RC and its variants ash,dash,bash,zshell,pdksh,tcsh,es ...), Awk,tcl/tk,expect, Perl,python, wait. This interpreter is obviously a subprocess of the current shell. If this interpreter is the same shell as the current shell, such as bash, then it is the child shell of the current shell, and the commands in the script are executed in the child shell environment without affecting the current shell's environment.

is the Q2:shell script executed as a separate process?

A: No, the shell script itself cannot be a process. As mentioned above, the shell script is interpreted by a shell interpreter and runs the commands in it. This shell interpreter is a separate process, and the external commands in the script are run sequentially as separate processes. That's why PS can't find the name of the running script. As an alternative, you can call the script like this:

sh script-name

The shell interpreter "sh" is called explicitly as an external command, and Script-name as the command line parameter of the command can be PS to us.

Also, if you have a pidof command available on your system, it can find the process ID of the shell script process (which should actually be the child shell process that executes the shell script):

pidof -x script-name

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.