Linux Shell Programming (1) – Basic functions of Bash: aliases, shortcuts, historical commands, output redirection, pipes, special symbols in Shell

Time:2019-8-13

Basic Functions of Bash

I. Order aliases

1. The order in which the order of order takes effect

  1. Execute commands executed using absolute or relative paths
  2. Execution alias
  3. Execute Bash internal commands
  4. Execution according to$PATHThe first command found in the directory order defined by the environment variable

2. View aliases

Command format

Alias [alias]

Example

[vagrant/tmp] ]$alias
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -AlhF --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
[vagrant/tmp] ]$alias ls
alias ls='ls --color=auto'
[vagrant/tmp] ]$alias cp
-bash: alias: cp: not found

3. Setting aliases

Command format

Alias alias alias ='command parameter'

Example

[vagrant/tmp] ]$alias cat='cat -n'
[vagrant/tmp] ]$alias less='less -mN'
[vagrant/tmp] ]$alias
alias cat='cat -n'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias less='less -mN'
alias ll='ls -AlhF --color=auto'
alias ls='ls --color=auto'
alias vi='vim'

Setting aliases to take effect permanently

In the above command line, aliases can only be used in the current bash, and once logged out, aliases will become invalid.
To make the alias permanent, you need to add the alias~/.bashrcConfiguration file.

  • ~/.bashrcfile
# some more ls aliases
alias grep='grep --color=auto'
alias ll='ls -AlhF --color=auto'
alias la='ls -A'
alias l='ls -CF'
alias vi='vim'
alias cat='cat -n'
alias less='less -mN'

4. Delete aliases

Command format

Unalias alias

Example

[vagrant/tmp] ]$alias
alias cat='cat -n'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias less='less -mN'
alias ll='ls -AlhF --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
[vagrant/tmp] ]$unalias grep
[vagrant/tmp] ]$unalias cat
[vagrant/tmp] ]$alias
alias l='ls -CF'
alias la='ls -A'
alias less='less -mN'
alias ll='ls -AlhF --color=auto'
alias ls='ls --color=auto'
alias vi='vim'

Deletion of aliases takes effect permanently

As with setting an alias, to permanently delete the alias, remove the alias from~/.bashrcDelete from the configuration file.

2. Common shortcuts

Ctrl + cMandatory cessation of the current order
Ctrl + lClear screen
Ctrl + aMove the cursor to the beginning of the command line
Ctrl + eMove the cursor to the end of the command line
Ctrl + uDelete from the cursor position to the beginning of the line
Ctrl + zTo issue orders and put them in the background for execution
Ctrl + rSearch in Command History

III. Historical Order

1. Command format

History [option] [history command saves files]

2. Options

-cEmpty historical orders
-wWrite the history command in the cache to the history command to save the file~/bash_history

3. Number of History Preservation Items

  • History commands save 1000 by default and can be configured in environment variables~/.bashrcModification
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

4. Call of historical orders

  • Use the up and down arrows to invoke previous historical commands
  • Use!nRepeated execution of historical order n
  • Use!!Repeat the previous command
  • UseString!Repeat the last command that begins with this string

IV. Output redirection

1. Standard Input and Output

equipment Device File Name File descriptor type
keyboard /dev/stdin 0 Standard input
Monitor /dev/stdout 1 standard output
Monitor /dev/stderr 2 Error Output

2. Output redirection

Standard output redirection

  • Command > File

Store the correct output of the command in a specified file or device in an overlay manner.

  • Order > Document

In addition, the correct output of the command is stored in the specified file or device.

Error output redirection

  • Error Command 2 > file

The error output of the command is stored in a specified file or device in a overlay manner.

  • Error Order 2 > Document

Store the wrong output of the command in a specified file or device in an additive manner.

Correct output and error output are saved at the same time

  • Command > File 2 > & 1

The correct output and error output of commands are stored in a specified file in a overlay manner.

  • Order > > Document 2 > & 1 (Commonly used)

In addition, the correct output and error output of the command are stored in the specified file.

  • Command &> File

The correct output and error output of commands are stored in a specified file in a overlay manner.

  • Order &> Document (Commonly used)

In addition, the correct output and error output of the command are stored in the specified file.

  • Order > Document 12 > Document 2 (Commonly used)

Append the correct output of the command to file 1 and the wrong output to file 2.

# Store the correct output of shell. sh running in access. log file and error output in error. log file
shell.sh >> access.log 2>> error.log

3. Input redirection

  • Command < Document

Input File Content as Command

# Execute statements in SQL files in MySQL
mysql -uroot -p < db.sql

# Statistical access. log file line number, word book, character number
wc < access.log
  4  24 130
# Actually the command can be executed without <
wc access.log
  4  24 130 access.log
  • Command Identifier
Order identifier
...
identifier

The content between two identical identifiers is used as input to the command.
Similar to PHPheredocGrammar.

V. Sequential execution of multiple orders

1. Sequential execution of multiple commands

Multi-command executor format Effect
; Command 1; Command 2 Multiple commands are executed sequentially, and there is no logical relationship between commands.
&& Command 1 & Command 2 Logic and
When command 1 is executed correctly, Command 2 is executed.
When command 1 is not executed correctly, Command 2 is not executed.
|| Command 1 | | Command 2 Logic or
When command 1 is executed incorrectly, Command 2 is executed.
When command 1 is executed correctly, Command 2 is not executed.
# Calculate the execution time of the intermediate compression command based on the difference between the two date outputs
date; tar -zcvf etc.tar.gz /etc; date

# Determine whether the first command is executed correctly based on whether yes or no is output
ls && echo yes || echo no

2. Pipeline character

Command format

Command 1 | Command 2

Use the correct output of command 1 as the operation object of Command 2

# View directory or file information under / etc / using less command
ll /etc/ | less -mN

# View the number of ports currently connected
netstat -an | grep ESTABLISHED | wc -l

# Remove comments and empty lines from the configuration file and generate a new configuration file
cat /etc/redis/redis.conf | grep -v "#" | grep -v "^$" > /etc/redis/redis6379.conf

# Find the file containing "max_children" in the / home directory
sudo find /home -type f -name '*' | xargs grep "max_children"

6. Special Symbols in Shell

1. Wildcards

wildcard Effect
Match an arbitrary character
* Match 0 or more arbitrary characters, that is, you can match anything.
[] Match any character in parentheses. For example:[abc]Represents matching any character in a/b/c
[-] Match any character in parentheses.-Represents a scope. For example:[a-z]Represents matching any lowercase letter
[^] Logically, matching is not any character in brackets. For example:[^0-9]Represents matching any character that is not a number
[vagrant/tmp] ]$ll
total 0
-rw-rw-r-- 1 vagrant vagrant 0 May  3 02:21 ab1
-rw-rw-r-- 1 vagrant vagrant 0 May  3 02:21 ab2
-rw-rw-r-- 1 vagrant vagrant 0 May  3 02:21 ab3
-rw-rw-r-- 1 vagrant vagrant 0 May  3 02:21 abc
-rw-rw-r-- 1 vagrant vagrant 0 May  3 02:21 abc.log
-rw-rw-r-- 1 vagrant vagrant 0 May  3 02:21 abd
-rw-rw-r-- 1 vagrant vagrant 0 May  3 02:21 abe
[vagrant/tmp] ]$ls abc
abc
[vagrant/tmp] ]$ls abc*
abc  abc.log
[vagrant/tmp] ]$ls ab?
ab1  ab2  ab3  abc  abd  abe
[vagrant/tmp] ]$ls ab[0-9]
ab1  ab2  ab3
[vagrant/tmp] ]$ls ab[0-9a-z]
ab1  ab2  ab3  abc  abd  abe
[vagrant/tmp] ]$ls ab[^a-z]
ab1  ab2  ab3

2. Other special symbols in Bash

Symbol Effect
'' Single quotation marks.
All special symbols in single quotation marks, such as$And `(back quotation marks) have no special meaning.
"" Double quotation marks.
There are no special symbols in double quotation marks, but$ ` (back quotation marks), and\Exceptions include the special meanings of “call variable value”, “quote command” and “escape character”.
`` Back quotation marks.
The content of inverted quotation marks is the system command, which is executed first in Bash.
$() Like inversion marks, they are used to refer to system commands. However, recommended use$()Because back quotation marks are very easy to read wrong.
# In the shell script,#Comments on the beginning of the line
$ The value used to invoke a variable, such as the value of the variable name, needs to be invoked in the form of $name to get the value of the variable.
\ An escape character.
Follow\Later special symbols will lose their special meaning and become ordinary characters. as\$Output$Symbols, not references as variables

Recommended Today

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]