A detailed explanation of Unix Shell program writing method

Source: Internet
Author: User
Tags command line execution sleep

What is a shell?

Any invention has an interface for users to use. The interface used by UNIX for users is the shell (DOS command is familiar, but Unix is much more powerful).

The shell provides users with an environment in which commands and parameters are entered and commands are executed.

UNIX provides a different shell for different needs. Now most of the UNIX support Bourneshell, the following tutorials to Bourneshell (BSH) as an example, step-by-step experience of the powerful Unix shell, the powerful charm, to achieve more convenient and flexible management, the purpose of the application of UNIX.

1.UNIX kernel and shell interaction methods

When UNIX is started, the program UNIX (kernel) is transferred into the computer's memory and remains in memory until the machine shuts down. During the boot process, program Init will run into the background until the machine shuts down. This program queries the file/etc/inittab, which lists the ports and their characteristics of the connection terminals. When an active terminal is found, the INIT program invokes the Getty program to display login login information on the terminal. (username and passwd), after the password is entered, Getty calls the login process, which authenticates the user based on the contents of the file/etc/passwd. If the user is authenticated, the login process sets the user's home directory to the current directory and hands control to a series of setup programs. The Setup program can be a specified application, usually the Setup program is a shell program, such as:/bin/sh is the Bourne Shell (command out, hehe).

After being controlled, the shell program reads and executes file/etc/.profile and. Profile. These two files are built into the system-wide and the user's own working environment. Finally, the shell displays a command prompt, such as $. (This is taking BSH as an example, if csh, for. Cshrc,ksh. Kshrc,bash for. BASHRC etc.) Note You may wish to view/etc/.profile and. Profile as DOS Autoexec.bat or Config.sys files.

When the shell exits, the kernel hands control to the INIT program, which restarts the automatic login process. There are two ways to get the shell to quit, one for the user to execute the exit command and two for the kernel (for example, Root with the KILL command) to issue a KILL command to end the shell process. After the shell exits, the kernel reclaims the resources used by the user and the program.

After user login, the relationship between user command and computer is: Command process--->shell program--->unix kernel---> Computer hardware. When the user enters a command, such as $ls,

The shell locates its executable file/bin/ls and passes it to the kernel for execution. The kernel produces a new child process call and executes the/bin/ls. When the program is finished, the kernel cancels

The child process and hands control to its parent process, the shell program. For example, execution:

$ps

This command will list the processes that the user is executing, that is, the shell program (in detail, don't worry now) and the PS program. If implemented:

$sleep & $ps

The first command produces a sleep child process that executes in the background. The PS command displays the child process when it executes.

Each time a user executes a command, a child process is generated. The execution of this subprocess has nothing to do with its parent process or shell, which allows the shell to do other work. (The shell just tells the kernel of the user's intent, and then what to do.) Now that Windows has a scheduled task (automatically performing a task at a fixed time, date), Unix has a very early feature, which is called the automatic execution of the shell. Some UNIX resources, such as Cron, can automate the shell program without the user's involvement (this function seems to be in the/var/spool/crotab directory).

The Crontab program is very useful for system administrators. The cron service is used to schedule programs to run at specific times (months, days, weeks, hours, minutes). We take the crontab of root as an example. The root user's crontab file is placed in the/var/spool/crontab/root in the following format:

(1) (2) (3) (4) (5) (6)

0 0 * * 3/usr/bin/updatedb

1. Minutes (0-60)

2. Hours (0-23)

3. Day (1-31)

4. Month (1-12)

5. Week (1-7)

6. The program to be run

Features and features of 2.Shell

1> Command Line interpretation

2> use reserved words

3> using shell metacharacters (wildcard characters)

4> can handle program commands

5> using input and output redirection and piping

6> Maintain some variables

7> Operating Environment control

8> supports Shell programming

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.