Shell basics of Linux Learning

Source: Internet
Author: User
Tags clear screen

Shell basics of Linux Learning

I. Shell Overview
1. Shell Overview:
Shell: Shell is a command line interpreter that provides users with a request to the Linux Kernel

Run the interface-level program of the program. You can use Shell to start, suspend, stop, or even write some programs. Similar

Equivalent to windows operation interface
Shell is also a powerful programming language, easy to write, easy to debug, and flexible. Shell is

Explain the execution script language. In Shell, you can directly call Linux commands.

2. Shell classification:
Bourne Shell: The Bourne Shell used since Unix 1979
The main file name of the Bourne Shell is sh.
C Shell: C Shell is mainly used in BSD unix systems. Its syntax is similar to that of C.
Shell has two main syntaxes: Bourne and C, which are incompatible with each other.
The Bourne family mainly includes sh, ksh, Bash (standard Shell), psh, and zsh;
The C family mainly includes csh and tcsh, which are mainly used for Unix
Bash: Bash is compatible with sh. Now Linux is used as the basic Shell for users.

3. Linux-supported Shell
Save it in the/etc/shells directory
[Root @ localhost ~] # Vi/etc/shells
/Bin/sh
/Bin/bash
/Sbin/nologin
/Bin/tcsh
/Bin/csh
[Root @ localhost ~] # Sh
Sh-4.1 # exit
Exit
[Root @ localhost ~] # Csh
[Root @ localhost ~] # Exit
Exit
[Root @ localhost ~] #

So how can we determine the current shell type we enter? Use echo $ SHELL to view the current Shell

[Root @ localhost ~] # Echo $ SHELL
/Bin/bash
Parent shell: the initial Shell
Sub-Shell: Shell called later

Ii. Script Execution Method
1. echo output command
Echo [Option] [Output content]
Option:
-E: supports character conversion controlled by backslash.
Control characters:
\ A: The warning sound is output.
\ B: The Return key, that is, the deletion key to the left.
\ N: linefeed
\ R: Enter key
\ T: Tab, that is, the Tab key
\ V: vertical Tab
\ 0nnn: output characters according to the octal ASCll code table. 0 indicates the number 0, and nnn indicates the three Octal numbers.
\ Xhh: output characters according to the hexadecimal ASCll code table. Hh indicates two hexadecimal numbers.
[Root @ localhost ~] # Echo "what do you want to do ?"
What do you want to do?
[Root @ localhost ~] # Echo-e "what do you want to \ B do ?"
What do you want t do?
[Root @ localhost ~] # Echo-e "\ x68 \ t \ x65 \ t \ x6c \ n \ x6c \ t \ x6f \ t"

The ASC Ⅱ code can also be output in hexadecimal format.

H e l
L o

Note that exclamation points have special meanings in the echo command.

[Root @ localhost ~] # Echo-e "\ e [1; 31 m Do you want do \ e [0 m"

Output Color: 30 m = Black, 31 m = red, 32 m = green, 33 m = yellow 34 m = blue, 35 m = foreign red, 36 m = blue, 37 m = white \ e [1;: Enable color \ e [0 m: Disable color

2. The first script

! /Bin/bashThe first Shell program

Echo-e "\ e [1; 34 m; Lin's sister fell down in the sky \ e [0 m"
Wq

[Root @ localhost ~] # Vi hello. sh

! /Bin/bash the script written below the nominal name is standard, pure Shell will not make an error, but when calling other programs will make an error, learn to write comments

3. Script Execution
(1). Grant the execution permission to run directly
Chmod 755 hello. sh
./Hello. sh relative path
(2). Execute the script through Bash call
Bash hello. sh # use bash to explain the command directly
If the script syntax is incorrect, you can use:
[Root @ localhost ~] # Vim hello. sh.
Note: Use English letters to write symbols.
[Root @ localhost ~] # Bash hello. sh
Call directly, or:
[Root @ localhost ~] # Chmod 755 hello. sh
[Root @ localhost ~] # Ll
Total usage 48
-Rwxr-xr-x. 1 root 91 July 28 11:25 hello. sh
The permission becomes executable.
[Root @ localhost ~] #./Hello. sh
Miss Lin in the sky
[Root @ localhost ~] #/Root/hello. sh
Miss Lin in the sky

III. Basic Bash Functions
1. Command alias and shortcut key
Command alias = Small name, another name
A. Some commands are particularly long, so we can take an alias for convenient operations.
B. After the operating system is upgraded, the command format has changed. We can use the old command to map the new command.

(1). alias
# View all command ALIASES IN THE SYSTEM
[Root @ localhost ~] # Alias
Alias cp = 'cp-I'
Alias l. = 'LS-d. *-color = auto'
...

(2). alias = 'original command'
# Set command alias
[Root @ localhost ~] # Vi/etc/passwd # Colors not supported
[Root @ localhost ~] # Vim/etc/passwd # color supported
We can:
[Root @ localhost ~] # Alias vi = 'vim'

(3). the alias takes effect permanently and deletes it.
Vi ~ /. Bashrc
# Write the environment variable configuration file (the configuration file that defines the operating environment)
Enter the. bashrc impromptu configuration under root.
Alias rm = 'rm-I 'where-I is used to ask whether to delete

(4). unalias alias
# Deleting aliases

(5). Order of command effectiveness
First, execute commands in absolute or relative paths.
Second Order: Execute alias
The third sequence: Execute Bash Internal commands.
Fourth Order: Execute the First Command found in the Directory Search sequence defined by the PATH environment variable
[Root @ localhost ~] # EchoPATH

(6). Common shortcut keys
Ctrl + c force terminate the current command
Ctrl + l clear screen
Ctrl + a move the cursor to the beginning of the command line
Ctrl + e move the cursor to the end of the command line
Ctrl + u Delete from cursor position to first line
Ctrl + z move the command to the background
Ctrl + r search in the duration command
[Root @ localhost ~] # ^ C

2. History commands
(1). History commands
History [Option] [history command to save files]
Option:
-C: Clear history commands
-W: write the history commands in the cache into the history command to save the file ~ /, Bash_history
Saving text is actually very small
[Root @ localhost ~] # Ls-alh
Total usage 104 K
-Rw ---. 1 root 7.2 K October 10 02:27. bash history
Can be saved
In addition, the history command saves the 1000 pick by default and can be modified in the environment variable configuration file/etc/profile.
[Root @ localhost ~] # Vi/etc/history
History Size = 1000
Can be directly modified
(2) Call History commands
Use the up and down arrows to call previous historical commands
Use "! N "repeats the n historical commands
Use "!!" Repeat the previous command
Use "! String "repeats the execution of the last command starting with this string
[Root @ localhost ~] # History # view all history Command records in the valid range
[Root @ localhost ~] # Service network restart
Closing interface eth0: [OK]
Disable the loop interface: [OK]
Pop-up loop interface: [OK]
Pop-up page eth0: [OK]
In this case, we can run the following command again:
[Root @ localhost ~] #! Ser
Service network restart

(3). Command and file completion
In Bash, commands and file population are both very convenient and common functions. When you enter commands or files,

Press the "Tab" key to automatically complete the task. If multiple related commands exist
We can: Press "Tab" twice. All related commands are displayed.
In fact, the command is fully dependent on the $ PATH command, and the file is fully dependent on the operation PATH.

3. Output redirection
(1). Overview:
Output redirection and input redirection:
Writing the results to a computer is called input, and displaying the results is called output.
Device File name file descriptor type
Keyboard/dev/stdin 0 standard input
Display/dev/sdtout 1 standard output
Display/dev/sdterr 2 standard error output

(2). Output redirection:
Type symbol function
Standard output redirection command> overwrite the file and output the correct command to the specified

> File to append the command to the specified file or device.

Standard Error output redirection error command 2> overwrite the file and output the command error to the specified

File or device error command 2> file to append the command error output to the specified file or device

Correct output and wrong Output commands> file 2> & 1 overwrite the correct and wrong Output commands

Save to the specified file at the same time
Command> file 2> & 1. output the correct and wrong commands in append mode.

To the specified file, run the command &> file to overwrite the correct output and error output in the same file. Run the command &> file in append mode, output the correct and wrong commands to the specified file. Command> file 1 2> file 2: append the command to save the correct output to file 1, save the error output to file 2.

Output redirection saves the output results to enable unattended operation.
[Root @ localhost ~] # Ifconfig> test. log
[Root @ localhost ~] # Cat test. log

Output result :...

As long as the command has output, it can be saved to the specified location. Note> the result will be overwritten if the result is not overwritten.

>>, If the target file does not exist, it is automatically created.
If the command does not exist or has an error, we should use 2> or 2> to save the error message, error message, and positive validation.

You must use different redirection formats. Note that there are no spaces.
Note:
[Root @ localhost ~] # Ls &>/dev/null
/Dev/null is null, and the files in it will be deleted directly. The saved logs will be destroyed and cannot be found.
Purpose: when writing a script, you only need to view the results without a process.

(3). input redirection
[Root @ localhost ~] # Wc [Option] [file name]
Option:
-C: count the number of bytes
-W count word
-L list of statistics rows
[Root @ localhost ~] # Wc
Abd
Ert

Ctrl + d
  2  2   6

Words are separated by spaces.

Command <the file uses the file as the command input and counts the number of bytes and words of the file.
Command <identifier
[Root @ localhost ~] # Wc <dy (equivalent to the end mark)

Asd

Erty
Dy
4 3 11
...
The identifier uses the content between identifiers as the command input.

4. MPS queue Operator
(1). Multiple commands are executed sequentially.
Multi-command operator format
Command 1; command 2 multiple commands are executed in sequence, and there is no logical connection between commands

& Command 1 & command 2 logic and (no data transmission)
If command 1 is correctly executed, command 2 will execute
If command 1 is incorrectly executed, command 2 will not execute

| Command 1 | command 2 logic or (no data transmission)
When command 1 is executed incorrectly, command 2 will execute
When command 1 is correctly executed, command 2 does not execute

Note that the semicolon is in English;
[Root @ localhost ~] # Ls & echo yes | echo no
Acces. log ....
Yes
[Root @ localhost ~] # Lsa & echo yes | echo no
-Bash: lsa: command not found
No
The order cannot be reversed: & echo yes | echo no
Command 1 can have multiple commands. If one of them is executed, command 1 is correct.

Run, | command 2 does not run
[Root @ localhost ~] # Ls | echo no & echo yes
Yes
[Root @ localhost ~] # Lsa | echo no & echo yes
No
Yes

(2) pipeline operator
Command Format:
[Root @ localhost ~] # Command 1 | command 2

The correct output of command 1 serves as the operation object of command 2.

Example:
[Root @ localhost ~] # Ls-l/etc | more
Split etc files into screens
[Root @ localhost ~] # Netstat-an | grep ESTABLISHEN
Tcp 0 52 192.168.0.156: 22 192.168.0.155: 1459 ESTABLISHEN

Extract network listening port information

[Root @ localhost ~] # Netstat-an | grep ESTABLISHEN | wc-l
1 # count the number of connection results

5. wildcard characters
(1). Overview: characters that can be used to match other symbols

Wildcard
? Match any character

Matches 0 or multiple arbitrary characters, that is, any content can be matched.

[] Matches any character in the brackets. For example, [abc] indicates that one of the characters must be matched: a or B.

Or c

[-] Match any character in the brackets.-represents a range. For example, [a-z] represents

Lowercase letters

[^] Non-logical, indicating that the match is not a character in brackets. For example, [^ 0-9] indicates that the matching is not

Number characters

Note: Apart from * matching multiple characters, only one character can be matched at a time.
Example:
[Root @ localhost ~] # Mkdir dy
[Root @ localhost ~] # Cd/root/dy
[Root @ localhost dy] # pwd
/Root/dy
[Root @ localhost dy] # touch dzp
[Root @ localhost dy] # touch dzp2
[Root @ localhost dy] # touch dzp3
[Root @ localhost dy] # touch dzp34
[Root @ localhost dy] # touch dzpcang
[Root @ localhost dy] # touch dzpbols
[Root @ localhost dy] # touch dzpa
[Root @ localhost dy] # ls
Dzp dzp2 dzp3 dzp34 dzpcang dzpbols
[Root @ localhost dy] # ls dzp
Dzp
[Root @ localhost dy] # ls dzp *
Dzp dzp2 dzp3 dzp34 dzpcang dzpbols
[Root @ localhost dy] # ls dzp?
Dzp2 dzp3
[Root @ localhost dy] # ls dzp [0-9]
Dzp2 dzp3
[Root @ localhost dy] # ls dzp [0-9] [0-9]
Dzp34
[Root @ localhost dy] # ls dzp [^ 0-9]
Dzpa
Note: Wildcards are mainly used to match file names or directories. To match file content, regular expressions are required.

(3) Other special symbols in Bash:
Symbol function
"Single quotes, all the special symbols in single quotes, such as" $ "and" '"and" \ "have no special meaning

"" Double quotation marks. Special characters in double quotation marks do not have special meanings, but "And" '"and" \ "have the special meanings of" Call variable value "," reference command "and" Escape Character ", and" anti-quotation marks. The content enclosed by quotation marks is a system command. In Bash, It will be executed first, and () Role 1

However, we recommend that you use $ (). It is easy to see the error in reverse quotation marks.

$ () Is used to reference system commands like anti-quotes.

# In shell scripts, # the line at the beginning represents the comment

Used to call the value of a variable. To call the value of a variable name, you must use Change the name

Amount Value

\ Escape character. All special characters following \ will be meaningless and become common characters. For example, $ will output "$

Rather than being referenced as a variable.

[Root @ localhost dy] # a = 123
[Root @ localhost dy] # echo
123
[Root @ localhost dy] # echo 'A'
A
[Root @ localhost dy] # echo ""
123
[Root @ localhost dy] # a = ls
[Root @ localhost dy] # echo "$"
Ls
[Root @ localhost dy] # a = ls
[Root @ localhost dy] # echo" A "dzpdzp2dzp3dzp34dzpcangdzpbolsdzpa is generally not" easy to confuse with "single quotes, we can use ()

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.