After learning linux common commands, we have to apply their knowledge! This blog will introduce how to linux common commands with them, where we assume that we have a common command well aware, I am not familiar with may concern, I see this blog Linux common commands .
First, the input and output redirection
Input redirection
Input redirection, is the file as input , the less work with.
Output redirection
Output redirection is divided into two, one is a standard output redirection (STDOUT, file descriptor 1) , the other is the error output redirection (STDERR, file descriptor 2) .
The output of a command, the default output on the screen , when the command was executed normally, we call output to standard output , but not at the time when the command is executed, will complain, when we say content output for error output . As shown below:
So, now the question is, we want to put these original to the content on the screen output to a file, then you need to re-locate the position of the output, that is, the so-called output redirection . Common redirect the output of the command is as follows:
command | effect |
---|---|
Command> file | The standard output file is written |
Command 2> file | The error output file is written without spaces and [2> between] |
Command >> file | The standard output is appended to the end of the file |
2 file command >> | The error output is appended to the end of the file without spaces between 2 and [>>] |
Command >> file 2> & 1 or command file & >> | The standard output and error output is appended to the end of the file |
Pipe command character (pipe symbol)
符号: | 【Shift + \】
作用:连接两个或者多个命令,如【命令A | 命令B | 命令C】
将前一个命令的输出,作为后一个命令的输入。
如:
-
找出所有被限制登录系统的用户,并统计该类用户数
-
以分页方式查看/etc目录下的文件情况【Enter 下一行】
-
重置root和abong的密码
-
发送邮件给abong用户,将正文和标题打包并发送【邮件=对象+标题+正文】
-
发送邮件给abong,输入正文,直到输入over才发送邮件
通配符
通配符,常用于查找文件,进行模糊查询。常见的通配符:
通配符 | 作用 |
---|---|
* | 匹配0个或者多个字符 |
? | 匹配单个字符 |
[0-9] | 匹配0~9之间的单个数字的字符 |
[a-z] | 匹配a~z之间的单个字母的字符 |
[A-Z] | 匹配A~Z之间的单个字母的字符 |
[abc] | 匹配a、b、c三个字符中的任意一个字符 |
转义字符
在Linux中,提供了转义字符来处理特殊的输入数据。
常用的转义字符如下:
转义字符 | 作用 |
---|---|
\ | 【转义单个字符】反斜杠后面的变量变为单纯的字符,如\$ |
‘’ | 【转义字符串】单引号,其中的变量变为单纯的字符串,如’\$$num’ |
“” | 【不转义字符串】双引号,保留其中的变量属性,不进行转义 |
`` | 反引号,返回反引号中命令的结果wc -l readme.txt |
环境变量
1、在Linux中,变量是计算机系统用于保存可变值的数据类型。一般都是大写的。使用变量时要加$。而环境变量,是用来定义系统运行环境的一些参数。
2、可通过env
命令查询系统的环境变量。也可以和我这样通过命令env | cut -d= -f1 | more
翻页查看环境变量名。
3、PATH环境变量介绍
可通过以下两种方式查看PATH变量的值
可以看到,PATH的值是5个目录。
PATH的作用:告诉Bash解释器(Shell解释器的一种)待执行的命令可能存放在哪个目录,然后Bash解释器就会逐个目录查找该命令,找到了,就执行该命令。
可以修改PATH的值,如添加目录
4、命令的执行步骤
- 判断命令是否以绝对路径或者相对路径执行,是的话,直接执行。
- 若不是,则检查命令是否是“别名命令”,是的话,也直接执行。
定义别名 :alias 别名=命令
取消别名:unalias 别名
- 若不是,则检查命令是内部命令还是外部命令。若是内部命令则直接执行。
- 若是外部命令,则在PATH环境变量所对应的目录中查找相应的命令名,找到再执行。否则不执行该命令。
注意:若PATH环境变量所对应的目录有根目录,则若是在根目录中存在与某些命令同名的木马文件,则在执行命令的时候即执行了这些木马文件,故不建议将根目录添加到PATH环境变量中。
5, the variable custom
user-defined variables can, usually uppercase defined. Custom Variables can be local, it may also be global.
Local variables : the user can use to create other users can not use.
Global variables : available to all users. [By export 变量名
the local variable to a global variable] [may cause due to insufficient permissions ordinary users can not use]
users can 变量名 = 变量值
define variables .