Some basic concepts of Shell

Source: Internet
Author: User

  

Interactive Shell and non-interactive shell

Interactive ModeShell waits for your input and immediately executes the command you submitted. This mode is called interactive because shell interacts with users. This mode is also very familiar to most users: logon, command execution, and logout. When you sign back, shell is terminated.

Shell can also run in another mode:Non-Interactive ModeIs executed in shell script (non-interactive) mode. In this mode, shell does not interact with you, But reads commands stored in files and runs them. When it reads the end of the file, shell is terminated.

Logon shell and non-Logon Shell

When init3 and mingetty spawn exit the logon prompt, you enter the account and password and a shell prompt appears. This process isLog on to Shell

Open the terminal window in the gnome graphic interfaceProgram, Compared with the logon shell, it isNon-Logon", You do not need to enter the user name and password.

In the login shell, only read/Etc/profile and ~ /. Bash_profile ,~ /. Bash_login and ~ /. Profile is the first in the order and the command is executed. Unless it is disabled by the -- noprofile option;
In a non-Logon shell, read only/Etc/bash. bashrc and ~ /. Bashrc

Input/Output redirection

The command reads data from standard input, and outputs data from standard output and standard error. Shell supports redirection of command input and output.

Redirect to MPs queue

Use | to connect the two commands, the standard output of the previous command is used as the standard input of the next command.

 
Ls-L | WC-l

List the current directory files in rows and calculate the number of rows, that is, the number of files.

Redirect to file

<Direct the standard input to a file (read data from the file)

> Direct the standard output to a file (if the file exists, overwrite the original file)

> Direct the standard output to a file. If the file exists, add the output content to the end of the file.

Cat abc.txt> def.txt

Merge the content of file abc.txt to file def.txt.

Redirect arbitrary file descriptor

For Linux, all operations on devices and files are performed using file descriptors. The file descriptor is a non-negative integer, which is an index value and points to the record table for each process in the kernel to open the file. When an existing file is opened or a new file is created, the kernel returns a file descriptor to the process. When you need to read and write files,

You also need to pass the file descriptor as a parameter to the corresponding function.

N <accept file descriptor N from File

N> N> direct the file descriptor n to the file

Major Application in redirection standard error

 
Cat dddcat: DDD: no such file or directory cat DDD 2>/dev/null

More targeted methods

Command> file1 2> file2 respectively redirects the standard output and the standard error command 2> & 1 sends the standard error to the same location as the standard output command 2> & 1> file sends the standard error to the standard output the same location, then redirect to filecommand 2> & 1 | tee file to send the standard output and standard errors of the command to the file and output them to the terminal.

The Tee command reads data from the standard input device, outputs its content to the standard output device, and saves it as a file. We can use tee to save the data imported from the pipeline into a file, or even several files at a time.

Sub-shell

Call another script to automatically generate a new shell environment. If you want to define and use some variables without affecting the current environment, you can use a sub-shell.

Format: (command) (the command to be executed is enclosed in parentheses)

 
(Export lang = en_us; command)
Temporarily set the variable Lang to en_us, and then execute the command
 
 

Source command

If you wantIn this shell EnvironmentExecute some text defined in the script file, you can use the source Command, as a short name, can also be written as:. Script (note the space between the dot and the script name)

 
The content in source config. Sh. config. shconfig. Sh will be executed in this shell Environment

If you do not need the source command, the config. Sh script will be executed in a new shell.

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.