linux专题(二)管道符、重定向与环境变量

简介

之前也用linux系统,但是只是浅层次的操作几个命令,所以萌生出系统学习的想法

本文主要用于学习 与文件读写操作有关的重定向技术的五种模式、 管道命令符 、 PATH变量及重要环境变量 ,本文的目录结构与参考目录大致相同,但是会包含一些实践

主要参考 《Linux就该这么学》

及命令大全https://www.linuxcool.com/

输入输出重定向

上一节学习了常用的命令,接下来要讲多个linux命令适当组合到一起协同工作

输入重定向是指把文件导入到命令中,而输出重定向则是指把原本要输出到屏幕的数据信息写入到指定文件中

标准输入重定向(STDIN,文件描述符为0):默认从键盘输入,也可从其他文件或命令中输入。

标准输出重定向(STDOUT,文件描述符为1):默认输出到屏幕。

错误输出重定向(STDERR,文件描述符为2):默认输出到屏幕。

[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l
total 12
drwxr-xr-x 3 root root 4096 Jul 13 14:04 A
-rw-r--r-- 1 root root   31 Jul 13 11:30 b.json
-rw-r--r-- 1 root root   22 Jul 13 13:01 c.json
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l c.json 
-rw-r--r-- 1 root root 22 Jul 13 13:01 c.json
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l d.json 
ls: cannot access d.json: No such file or directory

如上b.json输出信息此时为标准输出信息c.json文件不存在此时为错误输出信息,想要把两种信息写入到文件中,就要区别两种输出信息

输入重定向中用到的符号及其作用

符号 作用
命令 < 文件 将文件作为命令的标准输入
命令 << 分界符 从标准输入中读入,直到遇见分界符才停止
命令 < 文件1 > 文件2 将文件1作为命令的标准输入并将标准输出到文件2

输出重定向中用到的符号及其作用

符号 作用
命令 > 文件 将标准输出重定向到一个文件中(清空原有文件的数据)
命令 2> 文件 将错误输出重定向到一个文件中(清空原有文件的数据)
命令 >> 文件 将标准输出重定向到一个文件中(追加到原有内容的后面)
命令 2>> 文件 将错误输出重定向到一个文件中(追加到原有内容的后面)
命令 >> 文件 2>&1 或 命令 &>> 文件 将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)
#将man bash命令的结果输出到man_bash.txt中
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls
A  b.json  c.json
[root@iZ2ze9fp041k3gtah0ztnmZ test]# man bash > man_bash.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls
A  b.json  c.json  man_bash.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# more man_bash.txt 
BASH(1)                                      General Commands Manual                                     BASH(1)



NAME
       bash - GNU Bourne-Again SHell

SYNOPSIS
       bash [options] [file]

COPYRIGHT
       Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.

DESCRIPTION
       Bash is an sh-compatible command language interpreter that executes commands read from the standard input
       or from a file.  Bash also incorporates useful features from the Korn and C shells (ksh and csh).

       Bash is intended to be a conformant implementation of the Shell and Utilities portion of the  IEEE  POSIX
       specification (IEEE Standard 1003.1).  Bash can be configured to be POSIX-conformant by default.

输出覆盖写入

[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "hello zhao56" > man_bash.txt 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "hello zhao56" > man_bash.txt 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "hello zhao56" > man_bash.txt 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "hello zhao56" > man_bash.txt 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "hello zhao56" > man_bash.txt 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "hello zhao56" > man_bash.txt 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat man_bash.txt 
hello zhao56

输出追加写入

[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "hello zhao56" >> man_bash.txt 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "hello zhao56" >> man_bash.txt 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat man_bash.txt 
hello zhao56
hello zhao56
hello zhao56

标准和错误输出重定向

#标准输出写入到文件中去了,错误的输出重定向则把信息输出到屏幕上
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l man_bash.txt
-rw-r--r-- 1 root root 39 Jul 13 15:16 man_bash.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l man_bash.txt > man_bash_1.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l man_bash.txt 2> man_bash_1.txt
-rw-r--r-- 1 root root 39 Jul 13 15:16 man_bash.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls
A  b.json  c.json  man_bash_1.txt  man_bash.txt
#用一个不存在的文件测试,则标准输出没写入文件,错误输出则写入到文件中
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls
A  b.json  c.json  man_bash_1.txt  man_bash.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l xxxx.txt > man_bash_1.txt
ls: cannot access xxxx.txt: No such file or directory
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l xxxx.txt 2> man_bash_1.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat man_bash_1.txt 
ls: cannot access xxxx.txt: No such file or directory
#不想区分标准还是错误输出信息,将其都写入到文件中去
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l man_bash.txt &>> man_bash_1.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l xxxx.txt &>> man_bash_1.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat man_bash_1.txt 
ls: cannot access xxxx.txt: No such file or directory
-rw-r--r-- 1 root root 39 Jul 13 15:16 man_bash.txt
ls: cannot access xxxx.txt: No such file or directory

输入重定向

#输入重定向用的较少
[root@iZ2ze9fp041k3gtah0ztnmZ test]# wc -l < man_bash_1.txt 
3
[root@iZ2ze9fp041k3gtah0ztnmZ test]# wc -l < /etc/passwd
23

管道命令符

格式: 命令A | 命令B [|命令C]...

简介: 把前一个命令原本要输出到屏幕的信息当作是后一个命令的标准输入

#如之前的两个命令
[root@iZ2ze9fp041k3gtah0ztnmZ test]# grep /sbin/nologin /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
dockerroot:x:997:994:Docker User:/var/lib/docker:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@iZ2ze9fp041k3gtah0ztnmZ test]# wc -l 
#可以通过管道命令符合并为
[root@iZ2ze9fp041k3gtah0ztnmZ test]# grep /sbin/nologin /etc/passwd | wc -l
19

#将ls和more合并
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l /etc/ |more
total 1400
-rw-r--r--   1 root root       18 Jul 11 08:38 adjtime
-rw-r--r--.  1 root root     1529 Apr  1  2020 aliases
-rw-r--r--   1 root root    12288 May 21 03:56 aliases.db
drwxr-xr-x.  2 root root     4096 May 21 12:01 alternatives
-rw-------.  1 root root      541 Aug  9  2019 anacrontab
-rw-r--r--.  1 root root       55 Aug  8  2019 asound.conf
-rw-r--r--   1 root root        1 Oct 31  2018 at.deny
--More--

#ps、grep、管道符结合
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ps aux | grep bash
root       494  0.0  0.1 115680  2236 pts/0    Ss   09:10   0:00 -bash
root      1163  0.0  0.0 115544  1552 tty1     Ss+  Jun25   0:00 -bash
root      1347  0.0  0.0 112816   976 pts/0    S+   15:49   0:00 grep --color=auto bash

#显示系统中所有与bash相关的进程信息,结果同时输出到屏幕和文件
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ps aux | grep bash | tee result.txt
root       494  0.0  0.1 115680  2236 pts/0    Ss   09:10   0:00 -bash
root      1163  0.0  0.0 115544  1552 tty1     Ss+  Jun25   0:00 -bash
root      1381  0.0  0.0 112812   968 pts/0    R+   15:55   0:00 grep --color=auto bash
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat result.txt 
root       494  0.0  0.1 115680  2236 pts/0    Ss   09:10   0:00 -bash
root      1163  0.0  0.0 115544  1552 tty1     Ss+  Jun25   0:00 -bash
root      1381  0.0  0.0 112812   968 pts/0    R+   15:55   0:00 grep --color=auto bash

[root@iZ2ze9fp041k3gtah0ztnmZ test]# ps aux | grep bash > result.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat result.txt 
root       494  0.0  0.1 115680  2236 pts/0    Ss   09:10   0:00 -bash
root      1163  0.0  0.0 115544  1552 tty1     Ss+  Jun25   0:00 -bash
root      1385  0.0  0.0 112816   968 pts/0    S+   15:56   0:00 grep --color=auto bash
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ps aux | grep bash >> result.txt
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat result.txt 
root       494  0.0  0.1 115680  2236 pts/0    Ss   09:10   0:00 -bash
root      1163  0.0  0.0 115544  1552 tty1     Ss+  Jun25   0:00 -bash
root      1385  0.0  0.0 112816   968 pts/0    S+   15:56   0:00 grep --color=auto bash
root       494  0.0  0.1 115680  2236 pts/0    Ss   09:10   0:00 -bash
root      1163  0.0  0.0 115544  1552 tty1     Ss+  Jun25   0:00 -bash
root      1388  0.0  0.0 112816   972 pts/0    S+   15:57   0:00 grep --color=auto bash

命令行的通配符

配符 含义
* 任意字符
? 单个任意字符
[a-z] 单个小写字母
[A-Z] 单个大写字母
[a-Z] 单个字母
[0-9] 单个数字
[[:alpha:]] 任意字母
[[:upper:]] 任意大写字母
[[:lower:]] 任意小写字母
[[:digit:]] 所有数字
[[:alnum:]] 任意字母加数字
[[:punct:]] 标点符号
#查询以.json结尾的
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l *.json
-rw-r--r-- 1 root root 31 Jul 13 11:30 b.json
-rw-r--r-- 1 root root 22 Jul 13 13:01 c.json
#查询以.txt结尾的
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l *.txt
-rw-r--r-- 1 root root 160 Jul 13 15:37 man_bash_1.txt
-rw-r--r-- 1 root root  39 Jul 13 15:16 man_bash.txt
-rw-r--r-- 1 root root 460 Jul 13 15:57 result.txt
#查询只有一个字符结尾为.txt的文件
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l ?.txt
ls: cannot access ?.txt: No such file or directory
#查询只有一个字符结尾为.json的文件
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls -l ?.json
-rw-r--r-- 1 root root 31 Jul 13 11:30 b.json
-rw-r--r-- 1 root root 22 Jul 13 13:01 c.json
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls
A  b.json  c.json  man_bash_1.txt  man_bash.txt  result.txt
#创建A.json、B.json、C.json
[root@iZ2ze9fp041k3gtah0ztnmZ test]# touch {A,B,C}.json
[root@iZ2ze9fp041k3gtah0ztnmZ test]# ls
A  A.json  b.json  B.json  c.json  C.json  man_bash_1.txt  man_bash.txt  result.txt
#输出指定信息
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo file{1,2,3,4,5}
file1 file2 file3 file4 file5

常用的转义字符

常用转义字符

字符 解释
反斜杠(\) 使反斜杠后面的一个变量变为单纯的字符
单引号('') 转义其中所有的变量为单纯的字符串
双引号("") 保留其中的变量属性,不进行转义处理
反引号(``) 把其中的命令执行后返回结果
#双引号
[root@iZ2ze9fp041k3gtah0ztnmZ test]# PRICE=5
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "price is $PRICE"
price is 5

#反斜杠
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "price is $$PRICE"
price is 494PRICE
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo "price is \$$PRICE"
price is $5

#反引号
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo `uname -a`
Linux iZ2ze9fp041k3gtah0ztnmZ 3.10.0-1160.25.1.el7.x86_64 #1 SMP Wed Apr 28 21:49:45 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo `uname -a`  123
Linux iZ2ze9fp041k3gtah0ztnmZ 3.10.0-1160.25.1.el7.x86_64 #1 SMP Wed Apr 28 21:49:45 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux 123
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo `uname -a 12`  123
uname: extra operand ‘12’
Try 'uname --help' for more information.
123

重要的环境变量

在linux中变量名一般大写,命令一般小写这是一种约定俗成的规范

命令在linux中执行的四个步骤

  1. 判断用户是否以绝对路径或相对路径的方式输入命令(如/bin/ls),如果是的话则直接执行

  2. Linux系统检查用户输入的命令是否为“别名命令”,即用一个自定义的命令名称来替换原本的命令名称

  3. Bash解释器判断用户输入的是内部命令还是外部命令。内部命令是解释器内部的指令,会被直接执行;而用户在绝大部分时间输入的是外部命令,这些命令交由步骤4继续处理

#通过type判断命令是内部命令还是外部命令
[root@iZ2ze9fp041k3gtah0ztnmZ test]# type echo
echo is a shell builtin
[root@iZ2ze9fp041k3gtah0ztnmZ test]# type pwd
pwd is a shell builtin
[root@iZ2ze9fp041k3gtah0ztnmZ test]# type uptime
uptime is hashed (/usr/bin/uptime)
  1. 系统在多个路径中查找用户输入的命令文件,而定义这些路径的变量叫作PATH , 作用是告诉Bash解释器待执行的命令可能存放的位置 , PATH是由多个路径值组成的变量,每个路径值之间用冒号间隔
[root@iZ2ze9fp041k3gtah0ztnmZ test]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

常用环境变量

变量名称 作用
HOME 用户的主目录(即家目录)
SHELL 用户在使用的Shell解释器名称
HISTSIZE 输出的历史命令记录条数
HISTFILESIZE 保存的历史命令记录条数
MAIL 邮件保存路径
LANG 系统语言、语系名称
RANDOM 生成一个随机数字
PS1 Bash解释器的提示符
PATH 定义解释器搜索用户执行命令的路径
EDITOR 用户默认的文本编辑器
#创建用户变量
[root@iZ2ze9fp041k3gtah0ztnmZ test]# WORKDIR=/usr/local/test
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cd $WORKDIR/
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cd ..
[root@iZ2ze9fp041k3gtah0ztnmZ local]# cd  ..
[root@iZ2ze9fp041k3gtah0ztnmZ usr]# cd $WORKDIR/
[root@iZ2ze9fp041k3gtah0ztnmZ test]# pwd
/usr/local/test
#取消变量
[root@iZ2ze9fp041k3gtah0ztnmZ test]# unset WORKDIR
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cd $WORKDIR
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# cd $WORKDIR

注:直接在终端设置的变量,重启后就会失效,export WORKDIR可以把命令提升为全局变量,所有用户就可以使用了

posted @ 2021-07-13 17:01  zhao56  阅读(141)  评论(0编辑  收藏  举报