linux entry series 7-- pipe symbol, redirection, environment variable

Previous article we learned the basic linux command, if the combination of different commands can be multiplied improve work efficiency. This article will learn redirection, pipeline operators, wildcard escape character, as well as important environmental variables related knowledge, to lay the foundation for the later shell programming.

A, IO redirection

We have previously explained nearly 60 linux common commands, ls command which files are under previously explained to view the current directory

~~~[root@heimatengyun test]# lstest1.txt test2.txt~~~

After executing the command default result is output to the computer screen (monitor), but if we want to save the command execution results to a file, to facilitate future when needed can access at any time, how do we do it? This use to redirect knowledge.

1.1 Redirection Overview

Linux shell redirection means to modify the default implementation system commands, we can be understood as "change the input and output direction", divided redirect input and output redirection.

Since the redirection is to change the default input or output, and that the default input or output, what is it?

Only the relative program, the program reads the user input data supplied from a keyboard, the data stream is from the keyboard to the program, which is the standard * input ; calculation result data generated by the program directly presented on the display, i.e. the data stream from a program to monitor, which is the standard output *. Default standard input, output, as shown below:

file

The default data read from the keyboard to read data from the file, i.e. data stream from the file to the program, is * input redirection ; calculation result data generated by the program is not displayed on the display but instead into a file, that is, the data stream from the program to a file, is to redirect the output *.

file

There are many computer hardware, common keyboard input device, a mouse, a microphone, a pen tablet, an output device with a display, a projector, a printer or the like. However, in Linux, generally refers to the standard keyboard input device, an output device generally refers to the standard display.

Mentioned before, * everything is a file in Linux , including all computer hardware input and output devices, including keyboard, monitor, etc. is a file. In order to express and differentiate file has been opened, Linux will be allocated for each file a ID, which is an integer, is called a file descriptor * (File Descriptor).

File descriptor associated with the input and output in the following table:

File descriptor file name Types of
hardware
0
stdin
Standard input file
hard disk
1
stdout Standard output file
monitor
2
stderr The standard error output file monitor

Linux programs in the implementation of any form of IO operations, are reading or writing a file descriptor. Only a file descriptor associated with a file and integer open, it may be detained on a normal hard disk files, FIFO, pipeline, terminal, keyboard, monitor, or even a network connection. stdin, stdout, stderr default are open, the redirection process, 0,1,2 three file descriptors can be used directly.

1.2 Redirection classification

Redirected into input and output redirection. In short, the input redirection is to import the file to the command, the output is supposed to redirect the output to the screen information is written to the specified file. Usually work, a higher relative to the input redirection, output redirection frequency, output redirection in turn subdivided into a standard output redirection and redirect the error output, output redirection is divided into: Clear writing and two additional modes.

On the standard output and error Consider the following example:

~~~[root@heimatengyun ~]# ls test/test1.txt test2.txt[root@heimatengyun ~]# ls xxxls: cannot access xxx: No such file or directory~~~

With the ls command to see the development of file information in the directory, if there is a folder contents will output the file owner, owning group, and other information in the file size and file folder, which is the command ls * Standard output information . But if you look at a file folder does not exist, the file does not exist error message prompts, it is the ls command error output *. If you want to output to the information screen on top of the original written directly to a file instead of to the screen, we must distinguish between the two output information.

1.2.1 Input redirection

Input redirection symbols related to the following table and action

symbol
effect
Command <file
The standard input file as a command
Command << delimiter
Reads from standard input until it encounters decomposition operator stopped
Command <Document 1> File 2 The command file as a standard input and standard output to the file 2

Input redirection with relatively little effect is to redirect input directly into the command file. / Etc / passwd file stores user information systems, a row a user. The following example demonstrates imported into the wc command by entering redirection this file, count the number of users.

~~~ [root @ heimatengyun test] # wc -l </ etc / passwd39 ~~~

1.2.2 Output Redirection

Output redirection symbols used in the following table and action

symbol
effect
Command 1> File
The standard output redirected to a file (original file data is empty), 1 can be omitted
Command 2> file
The error output redirected to a file (empty the original file data)
1 file command >>
The standard output redirected to a file (later added to the original content), 1 can be omitted
2 file command >>
The error output redirected to a file (appended to the back of the original content)
Commands & file >>
The standard output and error output common written to the file (added back to the original content)
Command >> file 2> & 1 Ditto command: Command & file >>

For redirects the standard output mode, do not write a file descriptor is generally omitted, and the file descriptor error output mode 2 is necessary to write.

1.2.3 Output redirection Case

通过man命令查看ls命令的使用方法,并将输出信息重定向到ls.txt文件中,然后就可以使用cat命令查看ls.txt文件的信息。

~~~[root@heimatengyun test]# lstest1.txt test2.txt[root@heimatengyun test]# man ls > ls.txt[root@heimatengyun test]# lsls.txt test1.txt test2.txt[root@heimatengyun test]# cat ls.txtLS(1) User Commands LS(1)NAMEls - list directory contentsSYNOPSISls [OPTION]... [FILE]...DESCRIPTION...省略部分内容~~~

接下来我们演示清空写入和追加写入的区别,先通过覆盖写入模式向ls.txt文件(原本有ls的帮助信息内容)写入一行数据,查看内容变化,然后再通过追加写入模式向文件写入一次数据,再查看文件内容的变化

~~~[root@heimatengyun test]# echo "wellcome" > ls.txt[root@heimatengyun test]# cat ls.txtwellcome[root@heimatengyun test]# echo "write message again" >> ls.txt[root@heimatengyun test]# cat ls.txtwellcomewrite message again~~~

可以看到覆盖模式将清空文件原有内容,追加模式则在原有内容后面添加数据。

1.2.4 标准输出和错误输出区别

虽然都是重定向技术,不同命令的标准输出和错误输出还是有区别的。如果一个命令执行成功,通过标准输出到文件是没有问题的,但是如果要错误输出重定向到文件是不会成功的,依旧会显示信息到屏幕。反之,如果一个命令执行失败,通过错误输出到文件是没有问题的,但是如果要输出到标准输出是不会成功的,依旧会显示到屏幕。

通过ls命令查看一个已经存在的文件,并将信息重定向到ls.txt文件,查看该文件可以看到成功存入信息。将其错误输出重定向到ls-err.txt文件,由于查看的该文件存在,没有错误信息所以看到查询成功的文件信息依然显示在了屏幕上,而错误重定向的文件里没有内容。

~~~[root@heimatengyun test]# lstest1.txt test2.txt[root@heimatengyun test]# ls -l test1.txt-rw-r--r--. 1 root root 0 Nov 30 15:34 test1.txt[root@heimatengyun test]# ls -l test1.txt > ls.txt[root@heimatengyun test]# lsls.txt test1.txt test2.txt[root@heimatengyun test]# cat ls.txt-rw-r--r--. 1 root root 0 Nov 30 15:34 test1.txt[root@heimatengyun test]# ls -l test1.txt 2> ls-err.txt-rw-r--r--. 1 root root 0 Nov 30 15:34 test1.txt[root@heimatengyun test]# lsls-err.txt ls.txt test1.txt test2.txt[root@heimatengyun test]# cat ls-err.txt~~~

二、管道符

通过管道符可以把很多命令组合起来,提高工作效率。简言之管道符的作用就是:把前一个命令原本要输出到屏幕的标准正常数据当作后一个命令的标准输入。

管道符用|表示,使用格式为:*命令A|命令B|命令C...*

  • 案例1:统计被禁止登陆系统的用户数

~~~[root@heimatengyun test]# grep "/sbin/nologin" /etc/passwd |wc -l34~~~

通过“linux入门系列5--新手必会的linux命令”介绍的grep命令匹配/etc/passwd文件中的关键字“/sbin/nologin”查找出被限制登陆系统的用户,并将匹配结果输入到wc命令,统计匹配到的行数,即为被限制登陆系统的用户数。

  • 案例2:将文件内容中的小写字母替换为大写字母输出

~~~[root@heimatengyun test]# cat test1.txtwellcome[root@heimatengyun test]# cat test1.txt |tr [a-z] [A-Z]WELLCOME[root@heimatengyun test]# cat test1.txtwellcome~~~

通过cat命令读取test1.txt文件内容并导入到tr命令,通过tr命令将内容中的小写字母替换为大写字母。可以看到只是对读取后的内容进行替换,对原文件没有影响。

tr命令作用是替换文本文件中的字符,格式为:*tr [原始字符] [目标字符]*

很多时候想要快速地替换文本中的一些词汇,如果手工替换,难免工作量巨大,尤其是需要处理大批量内容的时候。这时tr命令就可以派上用场,通过管道符将文本内容传递给它进行替换操作即可。

ps:前文讲了近60个Linux命令,命令太多不可能一一涵盖,其余的命令将根据场景需求逐步以案例的形式分散到各文中进行演示。

三、通配符

通配符的概念在很多语言中都存在,比如java、c#等,其作用就是模糊匹配。

假设你在电脑上存放了很多小电影,某一天突然想看某位老师的电影作品,但是由于文件太多以至于记不清楚电影文件的名称了,只是依稀记得文件名包含了几个关键字,这时候你怎么快速找到对应的文件呢?

通配符就是面对这种场景而生,熟练使用通配符,再多电影都不迷路。通配符顾名思义就是通用的匹配信息的符号,主要包含以下几个:

符号
意义
*
匹配0个和多个字符

匹配单个字符
[0-9] 匹配0~9之间的单个数字字符
[123] 匹配1、2、3这三个指定数字中的任意一个数字
[abc] 匹配a、b、c三个字符中的任意一个字符

  • 案例1:匹配文件名以test开头的所有文件

~~~[root@heimatengyun test]# lstest1.txt test2.txt[root@heimatengyun test]# ls -l test*-rw-r--r--. 1 root root 9 Nov 30 20:43 test1.txt-rw-r--r--. 1 root root 0 Nov 30 15:34 test2.txt

~~~

  • 案例2:匹配文件名最后一位为1或3的所有文件

~~~[root@heimatengyun test]# lstest1.txt test2.txt[root@heimatengyun test]# ls -l test[13].txt-rw-r--r--. 1 root root 9 Nov 30 20:43 test1.txt

~~~

四、转义符

“linux入门系列5--新手必会的linux命令”提到,人和Linux内核之间的交互是通过在shell终端中执行相关命令来实现的,为了能更好地理解用户的表达,除了通配符、管道符,shell解释器还提供了特别丰富的转义字符来处理用户输入的特殊数据。

本文只抽取几个常用的通配符进行讲解,转义符及对应的功能如下:

转义符 作用

反斜杠,使后边的一个变量变为单纯的字符串
''
单引号,转义其中所有的变量为单纯的字符串
""
双引号,保留其中的变量属性,不进行转义处理

反引号,把其中的命令执行后返回结果

  • 案例1:输出美元$表示的价格

~~~[root@heimatengyun test]# PRICE=99[root@heimatengyun test]# echo "the price is $PRICE"the price is 99[root@heimatengyun test]# echo "the price is $$PRICE"the price is 12395PRICE

~~~

定义PRICE变量保存价格,然后通过echo命令输出,发现输出的不是预期结果。原因是Linux中$表示变量,$$则有特殊的作用,表示当前程序的进程ID号。这时就需要反斜杠来进行转义,去除其特殊功能,将这个提取符转义为单纯的文本。

~~~[root@heimatengyun test]# echo "the price is $$PRICE"the price is $99

~~~

  • 案例2:将命令执行结果值赋值给变量并输出

~~~[root@heimatengyun test]# uname -aLinux heimatengyun 3.10.0-123.el7.x8664 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x8664 x8664 x8664 GNU/Linux[root@heimatengyun test]# MYSYS=`uname -a`[root@heimatengyun test]# echo $MYSYSLinux heimatengyun 3.10.0-123.el7.x8664 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x8664 x8664 x8664 GNU/Linux

~~~

用uname命令查看当前操作系统信息,并赋值给变量MYSYS,然后输出变量值。更多变量相关知识将在下一篇文章中详细介绍,此处主要掌握反引号这个转义符。

五、环境变量

变量是计算机系统用于保存可变值的数据类型,*在Linux系统中,变量名称一般是大写的,这是一种约定俗成的规范*。直接通过变量名即可获得对应的变量值。

环境变量是一种特殊的变量,是操作系统要正常运行的前提,数百个环境变量协同工作才使得操作系统能正常为用户提供服务。然而我们没有必要去全部学习和掌握所有数百个环境变量,只需要学习一部分常用的即可。

5.1 查看环境变量之env命令

一般通过env命令查看环境变量名

~~~[root@heimatengyun test]# envXDGSESSIONID=2HOSTNAME=heimatengyunSELINUXROLEREQUESTED=TERM=linuxSHELL=/bin/bash...省略部分内容

~~~

用echo命令查询环境变量值

~~~[root@heimatengyun test]# echo $SHELL/bin/bash

~~~

*Linux作为一个多用户多任务操作系统,能够为每个用户提供独立的工作环境,因此,一个相同的变量会因为用户身份的不同而具有不同的值*。

案例:使用不同用户查看HOME环境变量的值

~~~[root@heimatengyun ~]# echo $HOME/root[root@heimatengyun ~]# su - testLast login: Sat Nov 30 22:39:50 CST 2019 on pts/0[test@heimatengyun ~]$ echo $HOME/home/test[test@heimatengyun ~]$ exitlogout[root@heimatengyun ~]#

~~~

案例中先使用root用户查看$HOME的值,然后切换到test用户再次查看$HOME,从试验结果上看相同环境变量值却是不一样的。

注意:关于用户切换命令su的用法,su test和su - test是有非常大区别的,如果不加-表示只是切换用户不切换shell环境,如果加上-则表示连同shell环境一起替换,此处无论是否切换shell环境,两个不同用户的$HOME值都不一样。

5.2 设置环境变量之export命令

变量是由固定的变量名与用户或系统设置的变量值两部分注册,因此我们完全可以根据工作需要自行创建变量。

以下案例演示创建一个名称为$MYDIR,值为/etc/profile.d/ 目录的自定义变量,这样我们只需要通过该变量,就可以很方便的进入到值对应的目录。

~~~[root@heimatengyun test]# MYDIR=/etc/profile.d/
[root@heimatengyun test]# echo $MYDIR/etc/profile.d/[root@heimatengyun test]# pwd/root/test[root@heimatengyun test]# cd $MYDIR[root@heimatengyun profile.d]# pwd/etc/profile.d

~~~

此时创建的变量$MYDIR不具有全局性,作用范围有限,默认情况下不能被其他用户使用。

~~~[root@heimatengyun ~]# su test[test@heimatengyun root]$ echo $MYDIR

[test@heimatengyun root]$ exitexit

~~~

可以看到切换到test用户后,该变量没有值,并且通过env命令查看也未查到该变量。

如果要想让其他用户也可以使用该变量,则需要用export命令,将其提升为全局变量。注意export命令后的变量名不加$。

~~~[root@heimatengyun ~]# export MYDIR[root@heimatengyun ~]# envXDGSESSIONID=2HOSTNAME=heimatengyunSELINUXUSECURRENT_RANGE=MYDIR=/etc/profile.d/...省略部分内容

~~~

通过env命令也可以查到该变量,此时我们在切换到test用户查看是否可以使用

~~~[root@heimatengyun ~]# su test[test@heimatengyun root]$ echo $MYDIR/etc/profile.d/[test@heimatengyun root]$ exitexit

~~~

注意:再次强调一下,su test和su - test是有非常大区别的,如果不加-表示只是切换用户不切换shell环境。本例只是切换用户到test并没切换环境所以可以使用$MYDIR,如果使用su - test 把shell环境也替换的话,将无法使用自定义的$MYDIR环境变量。

5.3 常用的环境变量

下表列举几个重要常用的环境变量

变量名称
作用
HOME
用户家目录
SHELL
用户在使用的shell解释器名称
HISTSIZE
输出的历史命令记录条数
HISTFILESIZE 保存的历史命令记录条数
LANG
系统语言、语系名称
PATH
定义解释器搜索用户执行命令的路径

5.4 命令执行流程

Linux系统中一切皆文件,Linux命令也不例外。当用户执行一条命令之后,Linux系统到底发生了什么事情呢?

简单来说,命令在Linux中的执行分为以下4个步骤

(1)判断用户是否以绝对路径或相对路径的方式输入命令,如果是则直接执行,不是则进行第二步

(2)检查用户输入的命令是否有别名命令,如果有别名找到原命令,如果无则进行第三步

(3)bash解释器判断用户输入的是内部命令还是外部命令,如是内部命令则直接执行,外部命令则进行第四步

(4)系统在PATH环境变量中查找用户输入的命令,找到文件后执行命令。

*简单理解就是用户通过shell输入命令,shell解释器查找对应的命令文件并执行命令*。

注意:思考一下一个经典的问题,能否将当前目录(.)添加到环境变量PATH中呢?

Despite the current directory (.) Can be added to the PATH variable, so that in some cases allows users without having to enter the command path, however, so there is a big security risk. If the hacker such as a store of the same name with the command ls or cd virus file in the usual common directory / tmp, the user happens to execute these commands in the public directory, it is likely in the move.

Learn linux command execution flow, after taking over a Linux system, before executing a command to check for possible directory PATH variable, which is a good habit.

So far, most Linux commands have learned, accumulated knowledge about it, the next article, we will combine the knowledge learned earlier, officially entered the shell programming.

Released nine original articles · won praise 2 · Views 827

Guess you like

Origin blog.csdn.net/panchengjun/article/details/104021178