zl程序教程

您现在的位置是:首页 >  系统

当前栏目

linux的重定向、管道与环境变量PATH

Linux重定向 环境变量 path 管道
2023-06-13 09:14:59 时间

一、重定向

1.输出重定向:>

1.写入指定文件

[root@VM-8-8-centos lesson5]#  cat file.txt
[root@VM-8-8-centos lesson5]# echo "hello world" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
hello world
[root@VM-8-8-centos lesson5]# cat file.txt > test.c
[root@VM-8-8-centos lesson5]#  cat test.c
hello world

将 cat file.txt默认到显示器上的内容 显示到了 test.c文件中

2. 覆盖写

[root@VM-8-8-centos lesson5]# cat file.txt
hello world
[root@VM-8-8-centos lesson5]#  echo "you can see you" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you

file.txt文件的原来内容是 hello world,被变成了 you can see me 将原来的文件内容清空,再重新写

2.追加重定向 :>>

[root@VM-8-8-centos lesson5]#  echo "you can see you" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you
[root@VM-8-8-centos lesson5]# echo "you can see me" >> file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you
you can see me
[root@VM-8-8-centos lesson5]#  echo "you can see me" >> file.txt
[root@VM-8-8-centos lesson5]# cat file.txt
you can see you
you can see me
you can see me

把file.txt文件的内容 you can see me 打印后, 使用 >> 发现会在文件结尾 追加内容

3.输出重定向:<

1.键盘显示

[root@VM-8-8-centos lesson5]# cat
abcdefhgjkl
abcdefhgjkl

cat 不跟文件,默认从键盘读到什么就显示什么。

2.文件显示

使用 < 变为 从 指定文件中读取数据

```c

[root@VM-8-8-centos lesson5]# cat < file.txt
you can see you
you can see me
you can see me
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you
you can see me
you can see me

cat < file.txt 与 cat file.txt等价 cat < file.txt :从fille.txt文件中读取数据

4.重定向的一些认知误区

1. test.c只显示错误的

find /home -name test.c > msg.c

寻找 主目录中的 test.c文件 并重定向到 msg .c文件中

发现只能显示出权限不够而不能访问的 即错误的

2. msg.c只显示正确的

打印 cat msg.c 文件 只显示正确的

结论:显示器输出的信息中,有正确的,也有错误的, 只把正确的进行了重定向

3.分析

标准输出 和 标准错误输出 都是在显示器上打印,是两个不同的文件, 所以 >只重定向 标准输出

find /home -name test.c > msg.c 默认重定向 是 find /home -name test.c 1> msg.c 只不过把代码是1省略了 ,而代码1对应标准输出

4.显示出正确的

find /home -name test.c 2> msg.c 这里就代表将代码为2重定向到 msg.c文件,代码2代表标准输出 此时 test.c只显示正确的

二、管道

last指令

系统,历史上以时间为单位,登录服务器用户的个数

1.前五行数据的查询

取last数据的前五行

[root@VM-8-8-centos ~]# last | head -5
root     pts/1        103.57.12.38     Mon Oct  3 16:16   still logged in   
root     pts/0        106.41.249.118   Mon Oct  3 15:19   still logged in   
root     pts/0        103.57.12.38     Sun Oct  2 18:13 - 19:42  (01:29)    
root     pts/1        106.41.249.15    Sat Oct  1 14:09 - 15:59  (01:50)    
root     pts/0        103.57.12.38     Sat Oct  1 13:24 - 14:22  (00:58) 

last:登录的历史信息 | :被称为管道,用来数据传导 head:将文本行的前5行内容进行显示

1.用户登录次数

[root@VM-8-8-centos ~]#  last | grep root | wc -l
45

grep :按 行 为单位,进行关键字过滤 wc :统计行数 将 历史登录的信息(last)中的root用户的信息( grep root)统计行数(wc)

三、环境变量PATH

1.判断自己写的与系统的命令是否一致

使用 which 查询 ls 命令,发现是一个 64为可执行 程序,可以动态链接。

创建一个 mycmd.c文件,再通过vim编辑器,编辑一个c 通过 mycmd.c源文件 生成一个 可执行程序 mycmd

此时若直接输入 mycmd 则会报错,不带./ command命令找不到

此时我们会发现 为什么 我们自己写的就需要 ./ ,而系统命令就不需要。

结论:说明 系统自带的命令 不需要,而自己写的需要带上./

2.环境变量 PATH

1.环境变量 PATH概念

在系统中,可执行程序的搜索路径是保存在一个"全局的变量"PATH中,给系统提供命令的搜索路径,是环境变量的一种。

2.查看环境变量

PATH中保存了多条路径,路径之间用":"隔开 PATH: 从左到右依次进行程序搜索,找不到就就继续下一条路径,找到了就停下了,若所有路径都没找到,则爆出:command not found!

3.修改自己写的拷贝到 PATH中

使用 sudo是为了提高权限 将 mycmd拷贝到 /usr/bin目录中 此时 直接输入 mycmd 就可以显示出来它的内容

2.当前路径添加到环境变量中

此时就把 当前路径 lesson5导入PATH中 以此直接使用 mycmd