Linux shell Basics

Time:2021-10-25

1、 Shell overview

1.1 what is a shell

summary:The command line interpreter translates program instructions to the kernel and requests to call hardware functions.
Generally speaking, the command interpreter between the kernel and the user is equivalent to a translator.

Computer recognition: binary (dependent)ASCIICode table translation)
Provides users with a –LinuxKernel (send request) – run program
Essence: interface system level program
Purpose: start, suspend, stop and write programs
Programming language: the script language for interpretation and execution, which can be called directlyLinuxSystem command

Shell:LinuxCharacter interface in (accept command, convert command to binary)
Windows: YesshellAre you? The operation interface can be calledshell

Linux shell Basics

1.2 shell classification

1. Bourne shell: Unix has been using the Bourne shell since 1979. The main file name of the Bourne shell is sh.
2. C shell: C shell is mainly used in UNIX system of BSD version, and its syntax is similar to that of C language.
3. The two main syntax types of shell are Bourne and C, which are incompatible with each other.

echo $SHELLseeShell
Bash:BashAndshCompatible, now usedLinuxIs to useBashAs a basic userShell

LinuxSupported shell directoriesvi /etc/shells

1.3 main functions of shell

A scripting language that helps administrators simplify the management of Linux systems,Simplify management operations。 There are no complex functions like Java, PHP and other languages.

2、 Script execution mode

2.1 echo output command

Format:echo[options] [output content]

Options:e: character conversion with backslash control is supported

\A - output warning tone
\B - backspace
\N - line break
\R - enter
\T - tab, i.e. tab key
\V - vertical tab
\A - output warning tone
\B - backspace
\N - line break
\R - enter
\T - tab, i.e. tab key
\V - vertical tab
\0nnn - output characters in octal ASCII
\Xhh - output characters according to hexadecimal ASCII table
nnn - output characters in octal ASCII \Xhh - output characters according to hexadecimal ASCII table

echo “bols he cangls ni xihuan nayige”
echo -e “bols he cangls nib xihuan nayige”
echo -e “hellbo”
echo -e “htetlnltot”
echo -e “x68tx65tx6cnx6ctx6f”
Echo – e “E [1; 31mshell foundation e[0m”

Output color:

\e[1;xxm-Turn on color display;\e[0m-Turn off color display

30m=Black,31m=Red,32m=Green,33m=Yellow
34m=Blue,35m=Magenta,36m=Cyan,37m=White

2.2 script execution

Method 1:Give execution permission and run directly

chmod 755 hello.sh

./hello.sh(or absolute path execution)/root/hello.sh

Method 2:adoptBashCall execution script

bash hello.sh

Notes:#
First line:#!/bin/Bash
It’s not a comment. What’s belowLinuxStandard script program
If the script is completed using a pure shell statement, do not add#!/bin/Bash, no problem running
However, if the script calls other languages, an error will be reported.

3、 Basic functions of Bash

3.1 command alias and shortcut keys

3.1.1 alias

alias: view all command aliases in the system

alias ls = ‘ls –color=never’

Set alias(temporarily effective, invalid after restart or re login)

Alias alias = 'original command' (temporary)

Permanent effect
vi ~/.bashrc(more than one environment variable file) — write environment variable configuration file(~On behalf of the user (home directory)
vi /root/.bashrc–Increasealias ls = 'ls --color=never'(it will take effect permanently after the system is restarted again)
source .bashrc–Make the current environment variable setting effective (it does not need to be permanently effective after the system is restarted again)

Delete alias:
unalias ls

Order of command effectiveness

The first sequence executes the command of absolute path or relative path
Second sequential execution alias
The third sequence executes bash’s internal commands
The fourth sequence executes the first command in the directory order defined by the $path environment variable setting

3.1.2 shortcut keys

CTRL + C force termination
CTRL + L screen clearing is equivalent to clear
CTRL + a cursor quickly returns to the beginning of the line
CTRL + e cursor goes to the end of the line quickly
CTRL + U deletes from the cursor position to the beginning of the line
CTRL + Z puts the command in the background -- this command is used with caution
CTRL + R history command search

3.2 historical orders

Historical command refers to keeping the operated command in the system and calling it again when necessary.

cat ~./history, the command recorded in this file isLast login, after correct logoutSaved.
historyCommand view: there are not only previously saved commands, but also commands for new operations after this login. These commands will be written only after this correct exit~./bash_history

History [options] [save file with history command]

Options:

-C: clear history command
#It is not recommended to clear. Reason 1: when an error occurs in this operation, the historical command can be used for comparison and troubleshooting
#Reason 2: it is not recommended to clear the account. If you find that the historical command is cleared after logging in, you can judge that someone has logged in to the account or server
#Emptying is not recommended. Reason 3: even if the number of commands saved is large, the file size is not large, which will not cause much pressure on the server
-W: forcibly write the history command in the cache to the history command save file ~ /. Bash_ history

By default, 1000 history commands are saved, and the environment variable configuration file can be saved/etc/profileModify in(HISTSIZE)

Call of historical command:

Use the up and down arrows to invoke previous historical commands
Repeat the nth historical command with "! N"
Use "!" to repeat the previous command once
Use the "! String" to repeat the last command starting with the string once

asimplementservice network restartEnter after!ser, is to execute the same command.

Command and text completion

  1. stayBashIn,commandcatalogueAndfileCompletion is a very convenient and commonly used function. We just press when entering a command or file“Tab“Key will be completed automatically.
  2. The system relies on: $path for command completion

3.3 output / input redirection

Linux shell Basics

3.3.1 output redirection

Linux shell Basics

Linux shell Basics

About the meaning of 2 > & 1

  1. meaning:Redirect standard error output to standard output
  2. Symbol>&yesall in one, can not be separated, after separation, it is not the above meaning. For example, some people may think that 2 is standard error input, 1 is standard output, and > is a redirection symbol. Should “redirect standard error output to standard output” be written as “2 > 1”? Is that so? If you have tried, you will know that the writing method of 2 > 1 actually redirects the standard error output to a file named “1”
  3. It is also not allowed to write 2 & > 1

Q&A : It’s too troublesome to write “> log 2 > & 1” every time. Can you abbreviate it?
There are two abbreviations

1. &>log
2. >&log

The above two methods have the same semantics as “> log 2 > & 1”.
Is there any difference between & > and > & in the above two methods?
There is no semantic difference, but the first method is the best choice. Generally, the first method is used

command&> /dev/nullThrow the result of the command into thenull(the function is similar to that of the dustbin). Only execute, not display

3.3.2 input redirection (use less, just understand)

WC [options] [file name]

Options:

-c: Count bytes
-w: Count words
-l: Count rows

wc <file nameUse the contents of the file as input
wc << identifier

identifier Take the contents between identifiers as the input of commands

3.4 multi command executor and pipeline

Linux shell Basics

Application in script:

Command & & echo yes | echo no

The command is correctyeserrorno

be careful: && echo yes || echo noThe order cannot be reversed, otherwise the command will be output at the same time when there is an errorno yes

Paginated display of file contents:

More [file name]

Pipe symbol:

Command 1 | Command 2

Command 1Correct resultsAs the operation object of Command 2.

for example

LS - L > / etc / | more is used to display command execution results in pages
Netstat - an | grep established | WC - L is used to view the clients currently connected to the local machine and count the number of clients

3.4 wildcard (file name or directory name)

1.?   Match any character
2. * match 0 or any more characters
3. [] matches any character in parentheses
4. [-] matches any character in parentheses, - represents a range
5. [^] does not match any character in parentheses

For example:ls test*

Other special symbols in Bash:

'' single quotation marks, special symbols around the single quotation marks, such as $, & have no special meaning
"" double quotation marks. The special symbols around the double quotation marks have no special meaning, but "$", "`" and "\" are exceptions. It has the special meaning of "call variable value", "reference command" and "escape character"
``Backquotes. The contents enclosed in backquotes are system commands, which will be executed first in bash. Same as $().
$() is used to reference system commands
#In shell scripts, # the first line represents a comment
$is used to call the value of the variable. If you need to call the value of the variable name, you need to get the value of the variable in the way of $name
\Escape character, the special symbol following \ will lose its special meaning and become an ordinary character. If \ $, the "$" symbol will be output

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]