管道符、重定向、环境变量

管道符、重定向、环境变量

输入输出重定向

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

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
pos2@instance-20220901-0057:~$ man mkdir > mk.txt
pos2@instance-20220901-0057:~$ cat mk.txt
MKDIR(1)                                                      User Commands                                                     MKDIR(1)

NAME
       mkdir - make directories

SYNOPSIS
       mkdir [OPTION]... DIRECTORY...

DESCRIPTION
       Create the DIRECTORY(ies), if they do not already exist.

       Mandatory arguments to long options are mandatory for short options too.

>> 追加输入
pos2@instance-20220901-0057:~$ echo "wdnmd" >> mk.txt
pos2@instance-20220901-0057:~$ tail -n 5 mk.txt
       Full documentation at: <https://www.gnu.org/software/coreutils/mkdir>
       or available locally via: info '(coreutils) mkdir invocation'

GNU coreutils 8.30                                           September 2019                                                     MKDIR(1)
wdnmd

pos2@instance-20220901-0057:~/trydir$ ls -l xxx 2> err.txt
pos2@instance-20220901-0057:~/trydir$ cat err.txt
ls: cannot access 'xxx': No such file or directory

输入重定向的作用是把文件直接导入到命令中

1
2
3
# 统计文件行数
pos2@instance-20220901-0057:~/trydir$ wc -l < err.txt
1

管道命令符

命令A | 命令B 把A原本要输出到屏幕的数据作为B的标准输入

1
2
3
4
5
pos2@instance-20220901-0057:~/trydir$ cat /etc/passwd | grep "/sbin/nologin" | wc -l
29
# 跟这个同样效果,grep的用法
pos2@instance-20220901-0057:~/trydir$ grep "/sbin/nologin" /etc/passwd | wc -l
29
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
pos2@instance-20220901-0057:~/trydir$ ls -l /etc | more
total 856
drwxr-xr-x 3 root        root         4096 Aug 16 09:45 ModemManager
drwxr-xr-x 3 root        root         4096 Aug 16 09:45 NetworkManager
drwxr-xr-x 2 root        root         4096 Aug 16 09:46 PackageKit
drwxr-xr-x 4 root        root         4096 Aug 16 09:45 X11
-rw-r--r-- 1 root        root         3028 Aug 16 09:44 adduser.conf
drwxr-xr-x 2 root        root         4096 Sep  1 10:45 alternatives
drwxr-xr-x 3 root        root         4096 Aug 16 09:45 apparmor
drwxr-xr-x 7 root        root         4096 Aug 16 09:46 apparmor.d
--More--
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# ubuntu不能用--stdin这个参数。。
root@instance-20220901-0057:/home/pos2/trydir# useradd tp
root@instance-20220901-0057:/home/pos2/trydir# echo "tppassword" | passwd --stdin tp
passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]
# 可以用chpasswd
root@instance-20220901-0057:/home/pos2/trydir# echo tp:tppassword | chpasswd
root@instance-20220901-0057:/home/pos2/trydir#

# 顺便删了临时账户
root@instance-20220901-0057:/home/pos2/trydir# userdel -r tp
userdel: tp mail spool (/var/mail/tp) not found
userdel: tp home directory (/home/tp) not found
root@instance-20220901-0057:/home/pos2/trydir# cat /etc/passwd | grep "tp"
tss:x:106:111:TPM software stack,,,:/var/lib/tpm:/bin/false

命令行的通配符

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 匹配所有在/dev目录中且以sda开头的文件
pos2@instance-20220901-0057:~$ ls -l /dev/sda*
brw-rw---- 1 root disk 8,  0 Sep 13 11:24 /dev/sda
brw-rw---- 1 root disk 8,  1 Sep 13 11:24 /dev/sda1
brw-rw---- 1 root disk 8, 14 Sep 13 11:24 /dev/sda14
brw-rw---- 1 root disk 8, 15 Sep 13 11:24 /dev/sda15

# 匹配sda后面带一个字符的文件
pos2@instance-20220901-0057:~$ ls -l /dev/sda?
brw-rw---- 1 root disk 8, 1 Sep 13 11:24 /dev/sda1

pos2@instance-20220901-0057:~$ ls -l /dev/sda[0-9][0-9]
brw-rw---- 1 root disk 8, 14 Sep 13 11:24 /dev/sda14
brw-rw---- 1 root disk 8, 15 Sep 13 11:24 /dev/sda15

常用的转义字符

  • 反斜杠 \ 使\后面的一个变量变成单纯的字符串
  • 单引号 ’’ 转移其中所有的变量为单纯的字符串
  • 双引号 "" 保留其中的变量属性,不转义
  • 反引号 `` 把其中的命令执行后返回结果
1
2
3
4
5
6
7
pos2@instance-20220901-0057:~$ PRICE=5
pos2@instance-20220901-0057:~$ echo "price is $PRICE"
price is 5
pos2@instance-20220901-0057:~$ echo "price is \$$PRICE"
price is $5
pos2@instance-20220901-0057:~$ echo `uname -a`
Linux instance-20220901-0057 5.15.0-1016-oracle #20~20.04.1-Ubuntu SMP Mon Aug 8 07:20:11 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

重要的环境变量

命令在linux中的执行分为四步

  1. 判断是否以绝对路径或相对路径输入
  2. 检查是否是别名命令
  3. 判断是内部命令还是外部命令
  4. 在$PATH中查找命令
1
2
3
4
5
6
7
# 给命令起别名,可以添加默认选项
pos2@instance-20220901-0057:~$ alias rm='rm -i'
pos2@instance-20220901-0057:~$ rm mk.txt
rm: remove regular file 'mk.txt'? n
pos2@instance-20220901-0057:~$ alias rm
alias rm='rm -i'
pos2@instance-20220901-0057:~$ unalias rm
1
2
3
4
5
6
7
# type+命令 检查是内部命令还是外部命令
pos2@instance-20220901-0057:~$ type clear
clear is hashed (/usr/bin/clear)
pos2@instance-20220901-0057:~$ type cd
cd is a shell builtin
pos2@instance-20220901-0057:~$ type ls
ls is aliased to `ls --color=auto'
1
2
3
# PATH由多个路径组成,以:隔开
pos2@instance-20220901-0057:~$ echo $PATH
/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Licensed under CC BY-NC-SA 4.0
this is the way