Linux notes 2-redirection, wildcards and environment variables

Redirect

The standard input (STDIN) file description is 0. The default input is from the keyboard. The
standard output (STDOUT) file description is 1 The default output is to the screen.
Error output (STDERR) The file description is 2 The default output is to the screen
. Input and output during the interaction between commands and the server Below the example:

[root@bogon ~]# ls root    // 标准输入
anaconda-ks.cfg  lines.txt  test  test2.txt  test.txt  tstd   // 标准输出
[root@bogon ~]# ls CCC //标准输入
ls: cannot access CCC: No such file or directory  // 错误输出

Output redirection:
Command> Redirect file standard output to a file (overwrite the original content of the file)
Command 2> Redirect file error output to a file (overwrite the original content of the file)
Command >> Redirect file standard output to a file Medium (append to file)
command 2>> redirect file standard output to a file (append to file)
command >> file 2>$1 redirect both standard output and error output to a file (append to file )
Input redirection: the
command <file uses the file as the standard input of the
command. The command << resolution character is read from the standard input, and the
command is stopped only when the resolution character is encountered. <file 1> file 2 uses file 1 as the standard input of the command and Output standard to file 2

Example

[root@bogon tmpdir]# man bash > manbash.txt
[root@bogon tmpdir]# echo "hahaha" >test.txt
[root@bogon tmpdir]# cat test.txt
hahaha
[root@bogon tmpdir]# echo "hahah" >> test.txt
[root@bogon tmpdir]# cat test.txt
hahaha
hahah
[root@bogon tmpdir]# cat test.txt |wc -l
2
[root@bogon tmpdir]# wc -l <test.txt
8
[root@bogon tmpdir]# echo <<over
> hi 
> nice to meet you 
> Hi 
> over

Command wildcard

Sometimes we want to perform batch operations, this time we need to use wildcards. Such as viewing all directory attributes starting with sda

* 匹配另个或多个字符
?匹配任意单个字符
[0-9]匹配范围内数字
[abc]匹配范围内的任意字符
[root@bogon tmpdir]# ls /dev/vcs*
/dev/vcs   /dev/vcs2  /dev/vcs4  /dev/vcs6  /dev/vcsa1  /dev/vcsa3  /dev/vcsa5
/dev/vcs1  /dev/vcs3  /dev/vcs5  /dev/vcsa  /dev/vcsa2  /dev/vcsa4  /dev/vcsa6
[root@bogon tmpdir]# ls /dev/vcs[0-9]
/dev/vcs1  /dev/vcs2  /dev/vcs3  /dev/vcs4  /dev/vcs5  /dev/vcs6
\(反斜杠) 转义后面单个字符
'' (单引号)转义所有字符
"" (双引号)  变量依然生效
··(反引号 esc下的键)  执行命令语句
[root@bogon tmpdir]# PRICE=5
[root@bogon tmpdir]# echo "price is $PRICE"
price is 5
[root@bogon tmpdir]# echo "price is $$PRICE"
price is 2902PRICE
[root@bogon tmpdir]# echo "price is \$$PRICE"
price is $5
[root@bogon tmpdir]# echo 'price is \$$PRICE'
price is \$$PRICE
[root@bogon tmpdir]# uname -a
Linux bogon 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@bogon tmpdir]# echo echo `uname -a`
echo Linux bogon 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@bogon tmpdir]# 

PATH variable

The alias command is used to set the alias of the command. The format is: "alias alias=command". For
example, you can execute alias cp = "cp -i" if you are worried about overwriting the file by mistake. Every time you overwrite, you will be asked for the
unalias command to cancel Alias ​​format: "unalias alias"

[root@bogon tmpdir]# alias cp="cp -i"
[root@bogon tmpdir]# unalias cp

Everything in linux is a file, and commands are also files. Execution steps when entering a command to execute:
1. If it is an absolute path command, execute it directly.
2. Check whether it is an alias command.
3. When judged by bash, an internal command or an external command.
Internal command: an external command inside the interpreter
: Independent of the command file outside the interpreter
4. Find the command through the path defined in the $PATH variable and then execute it.
PATH environment variable display and modification

[root@bogon tmpdir]# PATH=$PATH:/root
[root@bogon tmpdir]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/apps/jdk/bin:/root/bin:/root

Careful and experienced operation and maintenance personnel will definitely be able to take over a linux server with delicate commands. Check whether there are any directories in the $PATH variable.

Environment variable

There are many environment variables in Linux that can be viewed using env as follows:

[root@bogon tmpdir]# env
XDG_SESSION_ID=15
HOSTNAME=bogon
TERM=vt100
SHELL=/bin/bash
HISTSIZE=1000

Variable definition and viewing
Variable definition: variable name = value

A user-defined variable cannot be used by other users, that is, the scope of the defined variable is the current user.
You can use export to upgrade local variables to global variables

[root@bogon tmpdir]# ETC="/etc"
[root@bogon tmpdir]# echo
[root@bogon tmpdir]# echo $ETC
/etc
[root@bogon tmpdir]# su hadoop
[hadoop@bogon tmpdir]$ echo $ETC
[hadoop@bogon tmpdir]$ su root
Password: 
[root@bogon tmpdir]# export ETC
[root@bogon tmpdir]# echo $ETC
[root@bogon tmpdir]# ETC="/etc"
[root@bogon tmpdir]# export ETC
[root@bogon tmpdir]# echo $ETC
/etc
[root@bogon tmpdir]# su hadoop
[hadoop@bogon tmpdir]$ echo $ETC
/etc

Guess you like

Origin blog.csdn.net/zhangxm_qz/article/details/109223575