09_Linux基础-SHELL-标准输入与标准输出-重定向-管道

1年前 (2022) 程序员胖胖胖虎阿
186 0 0

@TOC

09_Linux基础-SHELL-标准输入与标准输出-重定向-管道


作业
    编辑文本文件/home/text/file1,分4行输入bbb bbb  bbb、 ddd ddd ddd、aaa aaa aaa、ccc ccc ccc。 保存不要退出;
    在末行模式下打开一个新文件/text/file2;
    在file2中输入内容为eee eee eee在这一行内容之后,  将file1的内容读入到file2中,接着在命令模式下按照 abcde的显示顺序调整每行的位置,最后保存退出;
    同时编辑file1和file2,接着从file1切换到file2,然后 再快速切换回来,接着将file2的内容读到file1内容的末尾,显示文件file1内容的行号,复制3到6行到file1 内容的末尾;
    利用替换命令将全文的每行第一个ccc都替换为333;
    将全文中所有aaa都替换为111,撤销行号,最后不保 存退出;
[root@sanchuang-linux chenpeng]# mkdir /home/text2
[root@sanchuang-linux chenpeng]# cd /home/text2
[root@sanchuang-linux chenpeng]# mkdir /text2
[root@sanchuang-linux text2]# vim file1
:r /text2/file2
aaa aaa aaa
bbb bbb bbb
ccc ccc ccc
ddd ddd ddd
eee eee eee
[root@sanchuang-linux text2]# vim file1 /text2/file2
:n
:N
:set nu
:%s/ccc/333
:%s/aaa/111/g
:set nonu
:q!

练习1:
    1.进入/lianxi目录
# cd /lianxi
    2.复制/etc/passwd文件到当前目录下,然后使用vim编辑当前目录下的passwd
# cp /etc/passwd .  # vim passwd
    3.显示一下行号
:set nu
    4.复制第5到10行的内容,粘贴到末尾,删除最后一行的最后一个单词
:5 ; 6yy ; G ; p ; dw
    5.删除第9行和第10行
:9 ; 2dd
    6.将root改为gen
:%s/root/gen/g
    7.删除部分bash字符串
:%s/bash//gc
    8.快速到达第5行,然后再跳到第1行,再跳到最后一行
:5 ; :1 ; G
    9.剪切第6-7行,粘贴到末尾
:6 ; 2dd ; G ; p
    10.退出并且保存
:wq
    11.复制/etc/hosts文件到当前目录下,将第1列的ip地址复制,然后粘贴到文件的末尾
# /etc/hosts . ; # vim hosts ; ctrl+v ; y ; G ;p

一. SHELL-标准输入与标准输出

学习内容:重定向和文件目录高级操作

知识要点
输入输出重定向
管道及相关配套命令 数据提取及过滤
文件差异对比和补丁生成及打补丁

标准输入和标准输出

知识点1 标准输入和标准输出

---------------------------
输入输出及重定向
每个进程基本都会打开三个文件描述符
0: 标准输入(从键盘)
1: 标准输出 (输出到屏幕)
2: 标准错误输出 (输出到屏幕)

类型            设备文件        文件描述符(fd)    默认设备
标准输入      /dev/stdin      0                  键盘
标准正确输出     /dev/stdout     1                显示器
标准错误输出     /dev/stderr     2                显示器
-------------------------------------------------------
标准输入(stdin)和标准输出(stdout)
------------------------------------
python中 >>>help(print) file=sys.stdout 默认输出到屏幕(标准输出)
------------------------------------------------------------------
进程:正在进行的程序
---------------------
键盘 =》 标准输入 =》 终端程序 =》 标准输出     =》显示器
                            标准错误输出 =》显示器
----------------------------------------------------------
https://blog.csdn.net/xiaofei0859/article/details/78448493?utm_source=blogxgwz9
每个进程基本都会打开三个文件描述符
0:标准输入    (从键盘、语音、人脸识别……)
1:标准输出    (输出到屏幕)
2:标准错误输入(输出到屏幕)
---------------------------
文件描述符:fd  就是每个进程打开的文件

文件描述符

知识点1.1 文件描述符

文件描述符:
linux内核给每个进程都创造一个文件描述符,这个表里记载了这个进程所打开的所有文件,为了方便进程知道自己打开了哪些文件呢,就给每个文件都进行了标号,这个编号就是文件描述符。
此后,所有对文件的操作,就是用该编号。

linux下面一切皆文件

知识点1.2 linux下面一切皆文件

linux下面一切皆文件

disk磁盘

/dev目录下面基本都是块设备文件

[root@sanchuang-linux ~]# cd /dev
[root@sanchuang-linux dev]# cd disk
[root@sanchuang-linux disk]# ls
by-id  by-label  by-partuuid  by-path  by-uuid
[root@sanchuang-linux disk]# cd ..
[root@sanchuang-linux dev]# ls -ld sda1
brw-rw----. 1 root disk 8, 1 10月  6 17:16 sda1
[root@sanchuang-linux dev]# ls -ld sda2
brw-rw----. 1 root disk 8, 2 10月  6 17:16 sda2
------------------------------------------------
[root@sanchuang-linux dev]# cd net
[root@sanchuang-linux net]# ls
tun
[root@sanchuang-linux net]# 
----------------------------
对文件的操作就是对设备的操作
----------------------------
[root@sanchuang-linux ~]# cd /dev
[root@sanchuang-linux dev]# ls
stderr
stdin
Stdout
fd
……
---------------------------------
[root@sanchuang-linux ~]# cd /dev
[root@sanchuang-linux dev]# cd fd
[root@sanchuang-linux fd]# ls
0  1  2  255 
[root@sanchuang-linux fd]# ls -al
总用量 0
dr-x------. 2 root root  0 10月 16 10:40 .
dr-xr-xr-x. 9 root root  0 10月 16 10:40 ..
lrwx------. 1 root root 64 10月 16 10:40 0 -> /dev/pts/6    #(注:链接的意思,快捷方式 指向                                                                                    /dev/pts/6,标准输入到虚拟终端6)
lrwx------. 1 root root 64 10月 16 10:40 1 -> /dev/pts/6    #(注:标准输出到虚拟终端6)
lrwx------. 1 root root 64 10月 16 10:40 2 -> /dev/pts/6    #(注:标准错误输出到虚拟终端6)
lrwx------. 1 root root 64 10月 16 11:24 255 -> /dev/pts/6
[root@sanchuang-linux fd]# 
---------------------------------------

虚拟终端pts 真实终端tty

知识点1.3 虚拟终端pts 真实终端tty

[root@sanchuang-linux dev]# cd /dev/pts
[root@sanchuang-linux pts]# ls
0 1 2 3 4 5 6 ptmx
[root@sanchuang-linux pts]#

pts是别人连接到服务器的虚拟终端(远程控制)。别人远程连接会创建虚拟终端

pts虚拟终端

-----------------------------------------------------
Linux centos登录——》本地的终端shell环境  真实终端
xshell连接       ——》虚拟终端
--------------------------------
tty 本地真实终端
[root@sanchuang-linux dev]# cd /dev
[root@sanchuang-linux dev]# ls
tty

查看当前linux登陆的用户情况

知识点1.4 查看当前linux登陆的用户情况

查看当前linux登陆的用户情况 # w

[root@sanchuang-linux dev]# w
11:32:52 up 16:23, 4 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 0610月20 1:32m 0.60s 0.60s -bash (注:bash环境)
root pts/1 192.168.0.26 10:03 25:40 0.21s 0.21s -bash (注:bash环境)
root pts/2 10.122.155.164 08:52 2:29m 0.16s 0.13s vim -r a (注:删除环境)
root pts/6 192.168.0.26 10:40 4.00s 0.09s 0.01s w (注:输入w命令环境)

----------↓解释----------------------------------------------------------------------------

USER:登录用户

TTY:终端类型 terminal type 面向用户使用的接口

    `tty1:真实终端`

            tty1==>`直接登录进去 操纵的第一个终端`(真实终端)

            Linux里最多允许`6个`可以直接登录的终端(即`真实终端tty`)

            tty2~tty6 需要的时候使用

            `Alt+F1` tty1 回到第一个终端

            `Alt+F2` tty2 回到第二个终端

    `pts/1:虚拟终端`,给别人用的。数量可以有很多个。有远程登录过来的时候,就会创建1个终端

FROM:从哪里来

WHAT:做什么事

load average机器的拼接负载

------------------------------

[root@sanchuang-linux dev]# w
11:39:04 up 16:29, 4 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 0610月20 1:38m 0.60s 0.60s -bash
root pts/1 192.168.0.26 10:03 31:52 0.21s 0.21s -bash
root pts/2 10.122.155.164 08:52 2:35m 0.16s 0.13s vim -r a
root pts/6 192.168.0.26 10:40 0.00s 0.09s 0.00s w
[root@sanchuang-linux dev]# date
2020年 10月 16日 星期五 11:39:06 CST

load average机器的平均负载

---------------------------

本地终端(真实终端)的创建、切换

知识点1.5 本地终端(真实终端)的创建、切换

Alt+F2

本地最多能登录6个终端

Alt+F1~F6 切换本地登录的终端(真实终端)

-----------------------------------------

Alt+F2、Alt+F6

[root@sanchuang-linux dev]# w
11:42:06 up 16:32, 6 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 0610月20 1:41m 0.60s 0.60s -bash
root pts/1 192.168.0.26 10:03 34:54 0.21s 0.21s -bash
root pts/2 10.122.155.164 08:52 2:38m 0.16s 0.13s vim -r a
root pts/6 192.168.0.26 10:40 3.00s 0.09s 0.00s w
root tty2 - 11:40 1:50 0.01s 0.01s -bash
root tty6 - 11:41 13.00s 0.01s 0.01s -bash

如何拦截虚拟终端

知识点1.6 如何拦截虚拟终端

如何拦截虚拟终端 # kill -9 pid

-----------------------------------
[root@sanchuang-linux dev]# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.7 179304 13536 ?        Ss   10月15   0:07 /usr/lib/systemd/systemd --switched-root --system --dese
root        1450  0.0  0.2  25588  3996 tty1     Ss+  10月15   0:00 -bash
root       12692  0.0  0.2  34480  4444 ?        Ss   04:49   0:00 tmux
……………………………………………………
root       13025  0.0  0.2  25588  4048 pts/14   Ss+  05:14   0:00 -bash
root       13046  0.0  0.2  25588  4032 pts/15   Ss+  05:14   0:00 -bash
root       13286  0.0  0.2  25588  3940 pts/0    Ss+  05:28   0:00 -bash
root       13614  0.0  0.2  25588  4016 pts/3    Ss+  06:23   0:00 -bash
root       13635  0.0  0.2  25588  3956 pts/4    Ss   06:23   0:00 -bash
root       13657  0.0  0.4  48860  8596 pts/4    S+   06:24   0:00 vim /home/text/file2
[root@sanchuang-linux dev]# kill -9 13046    (注:杀死pid号13046的进程)
------------------------------------------------------------------------
ssh服务  22号端口  有端口肯定有服务
起服务一定要有端口
端口跟服务挂钩

Mysql默认端口3306

查看服务的默认端口

知识点1.7 查看服务的默认端口

怎样查看服务的默认端口 # less /etc/services

查看服务的默认端口

# less /etc/services
tcpmux          1/tcp                           # TCP port service multiplexer
tcpmux          1/udp                           # TCP port service multiplexer
rje             5/tcp                           # Remote Job Entry
rje             5/udp                           # Remote Job Entry
…………

知识点1.8 传输层协议 TCP、UDP

传输层协议 TCP、UDP

杀死连接 限制连接

知识点1.9 杀死连接 限制连接

杀死连接:

ps aux 查看pts的连接pid

然后使用: kill -9 xxx

-------------------------

限制连接:

iptables 防火墙

或使用其他ssh相关的限制

-------------------------

/proc目录

知识点2.0 /proc目录

/Proc目录 放一些正在运行的程序

\# ls /proc 正在运行的程序

[root@sanchuang-linux ssh]# ls /proc
………………………………………………(删了很多)
11     13657  1450   158  171  186  29  508  632  906  999        fb           loadavg       self           zoneinfo
13025  14438  15     168  182  23   40  6    859  982  diskstats  kmsg         partitions    tty
………………………………………………(删了很多)

查看进程打开了哪些文件描述符

知识点2.1 查看进程打开了哪些文件描述符 proc/1450/fd

查看进程打开了哪些文件描述符

[root@mysql-binary fd]# ls

0 1 2 255

[root@mysql-binary fd]# pwd

/proc/1680/fd

-----------------------------------------
[root@sanchuang-linux ssh]# cd /proc/1450
[root@sanchuang-linux 1450]# ls
attr        comm             fd        map_files   net            pagemap      schedstat     
………………………………(删了很多)
[root@sanchuang-linux 1450]# cd fd
[root@sanchuang-linux fd]# ls
0  1  2  255  3  4
[root@sanchuang-linux fd]# pwd
/proc/1450/fd

二. 重定向

重定向:改变标准输入、标准输出的方向就是重定向

--------------------------------------------------------------------------------------------

类型 操作符 用途

重定向标准输出 > 将命令的执行结果输出到指定的文件中,而不是直接显示在屏幕上

                            \>>            将命令执行的结果`追加输出到指定文件`

重定向标准输入 < 将命令中接收输入的途径由默认的键盘更改 为指定的文件

echo

知识点3.1 echo

echo -n 不接换行

echo -e 转义字符输出

[root@sanchuang-linux redirect]# echo "xxx"
xxx
[root@sanchuang-linux redirect]# echo -n "xxx"
xxx[root@sanchuang-linux redirect]#
[root@sanchuang-linux redirect]# echo "xx \n x1x1"
xx \n x1x1
[root@sanchuang-linux redirect]# echo -e "xx \n x1x1"
xx 
 x1x1
[root@sanchuang-linux redirect]# 
---------------------------------
echo -n 不接换行
echo -e 转义字符输出

重定向(改变原来的输出方式)

知识点3.2 重定向(改变原来的输出方式)

重定向(改变原来的输出方式)

[root@sanchuang-linux redirect]# echo "xxx" >file.txt
[root@sanchuang-linux redirect]# cat file.txt
xxx

脚本运行 标准错误输出、标准正确输出 (显示)

知识点3.3 脚本运行 标准错误输出、标准正确输出 (显示)

脚本一行一行 从上到下执行

[root@sanchuang-linux redirect]# vim test.sh
[root@sanchuang-linux redirect]# cat test.sh
t
date
[root@sanchuang-linux redirect]# sh test.sh
test.sh:行1: t: 未找到命令
2020年 10月 16日 星期五 14:49:40 CST

--------------------------------------------

注↑:标注错误输出 、正常输出

重定向

知识点3 重定向

重定向:改变标准输入、标准输出的方向就是重定向

--------------------------------------------------------------------------------------------

类型 操作符 用途

重定向标准输出 > 将命令的执行结果输出到指定的文件中,而不是直接显示在屏幕上

                            \>>            将命令执行的结果`追加输出到指定文件`

重定向标准输入 < 将命令中接收输入的途径由默认的键盘更改 为指定的文件

--------------------------------------------------------------------------------------------

操作如下

知识点3.4 # sh test.sh > test_result.txt

标准错误输出不会重定向到文件,正确输出会重定向到文件

[root@sanchuang-linux redirect]# vim test.sh
[root@sanchuang-linux redirect]# cat test.sh
t
date
[root@sanchuang-linux redirect]# sh test.sh
test.sh:行1: t: 未找到命令
2020年 10月 16日 星期五 14:49:40 CST
[root@sanchuang-linux redirect]# sh test.sh > test_result.txt
test.sh:行1: t: 未找到命令 (注:标准错误输出显示在屏幕上,不重定向到文件)
[root@sanchuang-linux redirect]# cat test_result.txt
2020年 10月 16日 星期五 14:50:32 CST(注:正确输出会重定向到文件)

\=\=\=\=\=\=\==\=\=\=\=\=\=\==\=\=\=\=\=\=\==\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\==\=\=\=\=\=\=\==\=\=\=\=\=\=\=

重定向 > 默认只会重定向标准输出

重定向 标准错误

知识点3 重定向 标准错误

标准错误:通过该设备报告执行出错信息

--------------------------------------------------------------------------------------------

类型 操作符

重定向标准错误 2>

                                                                        2>>

重定向标准输出和标准错误同一个文件 2>&1 或者>&或者 &>

重定向标准输出和标准错误不同文件 > 文件1 2 >文件2 (没教)

--------------------------------------------------------------------------------------------

操作如下

知识点3.5.1 # sh test.sh 2> test_result.txt

\# sh test.sh 2> test_result.txt 重定向标准错误 (注:与>效果相反)

正确输出显示在屏幕上,标准错误输出重定向到文件

只输出了错误的

[root@sanchuang-linux redirect]# sh test.sh 2> test_result.txt
2020年 10月 16日 星期五 14:52:33 CST (注:正确输出显示在屏幕上)
[root@sanchuang-linux redirect]# cat test_result.txt
test.sh:行1: t: 未找到命令 (注:标准错误输出重定向到文件)


知识点3.5.2 # sh test.sh &> test_result.txt

\# sh test.sh &> test_result.txt

把正确的错误的都重定向到 文件

[root@sanchuang-linux redirect]# sh test.sh &> test_result.txt
[root@sanchuang-linux redirect]# cat test_result.txt
test.sh:行1: t: 未找到命令
2020年 10月 16日 星期五 14:54:05 CST

与下面效果相同


知识点3.5.3 # sh test.sh > test_result.txt 2>&1

\# sh test.sh > test_result.txt 2>&1

把错误输出重定向向到标准输出

[root@sanchuang-linux redirect]# sh test.sh > test_result.txt 2>&1
[root@sanchuang-linux redirect]# cat test_result.txt
test.sh:行1: t: 未找到命令
2020年 10月 16日 星期五 14:55:16 CST

两者效果等同


知识点3.5.4 # sh test.sh > test_result.txt 1>&2

\# sh test.sh > test_result.txt 1>&2

把标准输出重定向错误输出

*输出到屏幕上 (*注:不重定向到文件)

[root@sanchuang-linux redirect]# sh test.sh > test_result.txt 1>&2
test.sh:行1: t: 未找到命令 (注:输出到屏幕上)
2020年 10月 16日 星期五 14:56:19 CST (注:输出到屏幕上)

[root@sanchuang-linux redirect]# cat test_result.txt
[root@sanchuang-linux redirect]#

使用重定向清空文件的几种方式

知识点3.6 使用重定向清空文件的几种方式

使用重定向清空文件的几种方式:

\# >stdout_test.txt

\# echo >stdout_test.txt (注:有空格)

\# echo "">stdout_test.txt (注:有空格)

\# :>stdout_test.txt

\# echo -n "" > stdout_test.txt

代码如下

--------------------------------------------------------------------------------------------

[root@sanchuang-linux redirect]# vim stdout_test.txt
[root@sanchuang-linux redirect]# >stdout_test.txt (注:清空)
[root@sanchuang-linux redirect]# cat stdout_test.txt
[root@sanchuang-linux redirect]# echo "aaa"> stdout_test.txt
[root@sanchuang-linux redirect]# cat stdout_test.txt
aaa
[root@sanchuang-linux redirect]# echo >stdout_test.txt (注:有空格)
[root@sanchuang-linux redirect]# cat stdout_test.txt

[root@sanchuang-linux redirect]# echo "aaa">stdout_test.txt
[root@sanchuang-linux redirect]# echo "">stdout_test.txt (注:有空格)
[root@sanchuang-linux redirect]# cat stdout_test.txt

[root@sanchuang-linux redirect]# echo "aaa">stdout_test.txt
[root@sanchuang-linux redirect]# :>stdout_test.txt (注:清空)
[root@sanchuang-linux redirect]# cat stdout_test.txt

[root@sanchuang-linux chenpeng]# echo -n "" > stdout_test.txt (注:清空)
[root@sanchuang-linux chenpeng]# cat stdout_test.txt
[root@sanchuang-linux chenpeng]#


知识点3.6.1 # echo -n "">stdout_test.txt

\# echo -n "">stdout_test.txt

echo默认情况会接一个换行符 不想换行 接-n

[root@sanchuang-linux redirect]# echo "aaa">stdout_test.txt
[root@sanchuang-linux redirect]# cat stdout_test.txt
aaa
[root@sanchuang-linux redirect]# echo "">stdout_test.txt
[root@sanchuang-linux redirect]# cat stdout_test.txt (注:有空格)

[root@sanchuang-linux redirect]# echo -n "">stdout_test.txt (注:清空,没有空格)
[root@sanchuang-linux redirect]#

重定向 > 与 >>

知识点3.7 重定向 > 与 >>

重定向> 与 >>

> 是以覆盖的方式写入

>> 是以追加的方式写入

代码如下

--------------------------------------------------------------------------------------------

[root@sanchuang-linux redirect]# echo "aaa" > stdout_test.txt

[root@sanchuang-linux redirect]# cat stdout_test.txt
aaa
[root@sanchuang-linux redirect]# echo "bb" >> stdout_test.txt
[root@sanchuang-linux redirect]# cat stdout_test.txt
aaa
bb
[root@sanchuang-linux redirect]#

重定向标准输入 read输入

知识点3.8 重定向标准输入 read输入

重定向标准输入 read输入

(注:用到了tmux)

While 循环

echo "$a $b $c"

脚本1 3个参数 3个参数
[root@sanchuang-linux redirect]# cat abc.sh
while read a b c
do
    echo "$a    $b    $c"
done < f1.txt
[root@sanchuang-linux redirect]# 

(注:窗口1)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# vim f1.txt
[root@sanchuang-linux redirect]# cat f1.txt
xx yy zz
aa bb cc

(注:窗口2)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# sh abc.sh
xx    yy    zz
aa    bb    cc

[root@sanchuang-linux redirect]# 

(注:窗口3)

echo "$a -- $b"

脚本2 2个参数 3个参数
[root@sanchuang-linux redirect]# vim abc.sh
[root@sanchuang-linux redirect]# cat abc.sh
while read a b
do
    echo "$a -- $b"
done < f1.txt
[root@sanchuang-linux redirect]#

(注:窗口1)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# vim f1.txt
[root@sanchuang-linux redirect]# cat f1.txt 
xx yy zz
aa bb cc
[root@sanchuang-linux redirect]# 

(注:窗口2)
──────────────────────────────────────────────
(注:默认空格分割参数,xx 和yy zz)
[root@sanchuang-linux redirect]# sh abc.sh 
xx -- yy zz
aa -- bb cc
[root@sanchuang-linux redirect]# 

(注:窗口3)

echo "line:$line"

脚本3 1个参数 3个参数
[root@sanchuang-linux redirect]# vim abc.sh
[root@sanchuang-linux redirect]# cat abc.sh 
while read line 
do
    echo "line:$line"
done < f1.txt
[root@sanchuang-linux redirect]# 

(注:窗口1)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# vim f1.txt
[root@sanchuang-linux redirect]# cat f1.txt 
xx yy zz
aa bb cc
[root@sanchuang-linux redirect]# 

(注:窗口2)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# sh abc.sh 
line:xx yy zz
line:aa bb cc
[root@sanchuang-linux redirect]# 

(注:窗口3)

echo "$a -- $b -- $c -- $d -- $e"

脚本4 5个参数 3个参数
[root@sanchuang-linux redirect]# vim abc.sh
[root@sanchuang-linux redirect]# cat abc.sh 
while read a b c d e 
do
    echo "$a -- $b -- $c -- $d -- $e"
done < f1.txt
[root@sanchuang-linux redirect]# 

(注:窗口1)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# vim f1.txt
[root@sanchuang-linux redirect]# cat f1.txt 
xx yy zz
aa bb cc
[root@sanchuang-linux redirect]# 

(注:窗口2)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# sh abc.sh 
xx -- yy -- zz --  -- 
aa -- bb -- cc --  -- 
[root@sanchuang-linux redirect]# 

(注:窗口3)
=================================

知识点3.9 上课练习 重定向标准输入 read输入

[root@sanchuang-linux redirect]# vim student_information.sh
[root@sanchuang-linux redirect]# cat student_information.sh 
echo "========学生信息管理系统==============="
while read name sex age
do
    echo "==姓名:$name, 性别:$sex, 年龄:$age=="
done < stu_info.txt
echo "========================================"
[root@sanchuang-linux redirect]# 

(注:窗口1 tmux)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# cat stu_info.txt
wenyao 18 f
fengcheng 78 m
chenpeng 80 m
[root@sanchuang-linux redirect]# 

(注:窗口2 tmux)
──────────────────────────────────────────────
[root@sanchuang-linux redirect]# sh student_information.sh 
========学生信息管理系统===============
==姓名:wenyao, 性别:18, 年龄:f==
==姓名:fengcheng, 性别:78, 年龄:m==
==姓名:chenpeng, 性别:80, 年龄:m==
========================================
[root@sanchuang-linux redirect]#

(注:窗口3 tmux)

三. SHELL-脚本练习及管道

知识点4.0 接受从键盘的输入 管道符号

脚本 接受从键盘的输入

要求:输入用户名和密码 创建相应的用户

需求分析:接受用户输入

                创建用户

                设置密码

-----相关知识点----------------------------------

管道符号 | :上一条命令的输出作为下一条命令的输入

yum list 默认输出到屏幕

yum list | grep tmux yumlist的输出 作为grep tmux命令的输入


创建用户 useradd wy
[root@sanchuang-linux ~]# useradd wy
[root@sanchuang-linux ~]# passwd wy
更改用户 wy 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
==============================================================
重新给用户wy设置密码
[root@sanchuang-linux ~]# echo "yyyzzz123" |passwd wy --stdin
stdin 标准输入

*4.1 初始脚本1

(注:# passwd在bash中执行时会在中间停止,让用户进行输入密码

    \# `为了防止shell停止使用参数--stdin`,让passwd介绍标准输入,echo是标准输出

    来自:https://blog.csdn.net/qq_31820885/article/details/78100587)
[root@sanchuang-linux chenpeng]# vim test.sh
read -p "请输入用户名:" username
read -p "请设置密码:" password

useradd $username
echo $password | passwd $username --stdin
echo $username
echo $password

[root@sanchuang-linux chenpeng]# sh test.sh
请输入用户名:cp1
请设置密码:123456
更改用户 cp1 的密码 。
passwd:所有的身份验证令牌已经成功更新。
cp1
123456
[root@sanchuang-linux chenpeng]# id cp1
uid=1009(cp1) gid=1009(cp1) 组=1009(cp1)
[root@sanchuang-linux chenpeng]# less /etc/passwd
root:x:0:0:root:/root:/bin/bash
………………
wy:x:1008:1008::/home/wy:/bin/bash
cp1:x:1009:1009::/home/cp1:/bin/bash

\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\==\=\=\=\=\=\==\=\=\=\=\=\=\=\==\=\=\=\=\=\==\=\=\=\=\=\=\==\=\=\=\=\=\=\==\=\=\=\==\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=

*4.2 python 与 shell 程序运行出错

python中如果程序运行中出现错误,后续操作会终止

shell 中如果出现错误,操作会继续向下执行

*4.3 优化脚本2

用户创建脚本优化:

1.让命令的标准输出和标准错误输出都输出到黑洞文件

echo $password | passwd $username --stdin &>/dev/null

2.密码隐藏 -s

read -s -p "请设置密码:" password

3.判断用户是否存在

条件语句:

if 条件 then

    `语句体1`

else

    `语句体2`

fi

*4.3 优化脚本2

[root@sanchuang-linux chenpeng]# vim test.sh
read -p "请输入用户名:" username

if cat /etc/passwd |grep $username &>/dev/null
then
    echo "$username is exist!"
else
    read -s -p "请设置密码:" password
    useradd $username &>/dev/null
    echo $password | passwd $username --stdin &>/dev/null
    echo -e "\n create $username ok!"
fi

----------------------------------------------------------
[root@sanchuang-linux chenpeng]# sh test.sh 
请输入用户名:cp1
cp1 is exist!
[root@sanchuang-linux chenpeng]# sh test.sh 
请输入用户名:cp8
请设置密码:
 create cp8 ok!
[root@sanchuang-linux chenpeng]# 

4.3.2 脚本优化3

[root@sanchuang-linux chenpeng]# vim test.sh
read -p "请输入用户名:" username

if cat /etc/passwd |grep $username &>/dev/null
then
    echo "$username is exist!"
else
    read -s -p "请设置密码:" password
    useradd $username &>/dev/null
    echo $password | passwd $username --stdin &>/dev/null
    echo -e "\n create $username ok!"
fi
          
(tmux 窗口1)                                                                                                       
──────────────────────────────────────────────
[root@sanchuang-linux chenpeng]# sh test.sh 
请输入用户名:chen5556
chen5556 is exist!
[root@sanchuang-linux chenpeng]# sh test.sh 
请输入用户名:chen7778
请设置密码:
 create chen7778 ok!
[root@sanchuang-linux chenpeng]# 

(tmux 窗口2)

4.3.4 脚本改善 最终版

[root@sanchuang-linux chenpeng]# id chen5557
uid=1019(chen5557) gid=1019(chen5557) 组=1019(chen5557)

--------------------------------------------------------

[root@sanchuang-linux chenpeng]# vim test.sh
read -p "请输入用户名:" username

if id $username &>/dev/null
then
   echo "$username is exist!"
else
   read -s -p "请设置密码:" password
   useradd $username &>/dev/null
   echo $password | passwd $username --stdin &>/dev/null
   echo -e "\n create $username ok!"
fi

(tmux 窗口1)
──────────────────────────────────────────────
[root@sanchuang-linux chenpeng]# sh test.sh 
请输入用户名:chen5556
chen5556 is exist!
[root@sanchuang-linux chenpeng]# sh test.sh 
请输入用户名:chen7777
请设置密码:
 create chen7777 ok!
[root@sanchuang-linux chenpeng]# 

(tmux 窗口2)

查看用户是否存在

*4.4 查看用户是否存在

[root@sanchuang-linux ~]# cat /etc/passwd |grep cp1
cp1: x:1009:1009::/home/cp1:/bin/bash

上一条命令返回值 $?

*4.5 上一条命令返回值 $?

$? 上一个命令的返回值

上一条命令返回值: 保存在 -->$?

为0是成功

为1是失败

命令返回值为0 表示执行成功

                    `1  执行失败`

                    `227  没有这条命令`

命令返回值 $?


*4.6 命令返回值 不精确的判断用户是否存在

不精确地判断用户是否存在

[root@sanchuang-linux ~]# cat /etc/passwd |grep cp1 (注:不精确地判断用户是否存在)
cp1: x:1009:1009::/home/cp1:/bin/bash
[root@sanchuang-linux ~]# echo $? (注:为0是成功)
0
[root@sanchuang-linux ~]# cat /etc/passwd |grep cp9
[root@sanchuang-linux ~]# echo $? (注:为1是失败)
1

\=\=\=\=\=\==\=\=\=\=\==\=\=\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\==

命令返回值 id

[root@sanchuang-linux ~]# id cp1
uid=1009(cp1) gid=1009(cp1) =1009(cp1)
[root@sanchuang-linux ~]# echo $?
0
[root@sanchuang-linux ~]# id cp9
id: “cp9”:无此用户
[root@sanchuang-linux ~]# echo $?
1
[root@sanchuang-linux ~]#

上一条命令返回值: 保存在 ——>$?

--------------------------------------------------------

命令返回值为0 表示执行成功

                    `1  执行失败`

                    `227  没有这条命令`

*4.7 精确的判断用户是否存在 (完善脚本)

想要精确匹配的话,就把过滤的范围缩小一点:

^wy: 以wy: 开头

^…:以什么开头,整行以什么开头

…$:以什么结尾,整行以什么结尾

[root@sanchuang-linux ~]# cat /etc/passwd |grep "^wy:"
wy: x:1008:1008::/home/wy:/bin/bash

[root@sanchuang-linux ~]# useradd wy:
useradd:无效的用户名“wy:”

wc命令 <重定向标准输入

知识点5.1 wc命令 <重定向标准输入

wc(字数统计)命令

格式:wc [选项]... 目标文件...

-l:统计行数

-w:统计字数 (前后都是空白的一组字符)

-c:统计字符数(可见和不可见的字符)

例:

\# wc /etc/hosts

4 23 185 /etc/hosts

(* 注:4行,23个单词,185个字节)

代码如下

---------------------------------------------------------------

[root@sanchuang-linux ~]# wc -l backup_log.sh (注:查看文件有多少行)
0 backup_log.sh
[root@sanchuang-linux ~]# wc < backup_log.sh (输入重定向)
0 0 0
[root@sanchuang-linux ~]# wc backup_log.sh
0 0 0 backup_log.sh

[root@sanchuang-linux lianxi]# wc -l sshd_config2 (注:查看文件有多少行)
148 sshd_config2
[root@sanchuang-linux lianxi]# wc < sshd_config2 (注:标准输入 输入重定向
148 559 4425
[root@sanchuang-linux lianxi]# wc sshd_config2 (注:wc(字数统计)命令)
148 559 4425 sshd_config2


知识点5.2 重定向 发信息 echo "hello brother" > /dev/pts/22

[root@sanchuang-linux ~]# w
17:20:37 up 19:51, 7 users, load average: 0.00, 0.00, 0.02
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 0610月20 7:20m 0.60s 0.60s -bash
root pts/7 192.168.0.30 14:30 42:37 0.09s 0.00s tmux
root pts/21 192.168.0.30 16:48 21.00s 0.14s 0.14s -bash
root pts/22 192.168.0.30 17:20 0.00s 0.02s 0.01s w

----------------------------------------------------------------------

\# echo "hello brother" > /dev/pts/22

重定向 发信息

xargs 管道符号

知识点5.3 xargs 管道符号

xargs 将前面命令的输出送给后面的命令作为参数使用

xargs 和 管道符号 一般配套使用

-------------------------------------------------

在当前路径查看.txt结尾的文件

find . -name "*.txt"

------------------------------------

管道 与 xargs

将管道前的输出作为参数赋给后面的命令

find . -name "*.txt" | xargs rm -rf

--------------------------------------------------------------------------------------

管道与xargs

xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。

[root@mysql-binary vim_test]# find . -name "*.sh" |xargs ls -al

[root@mysql-binary vim_test]# find . -name "*.txt" | xargs rm -rf

(注:ls -al 看文件详细信息)

代码如下

------------------------------------------------------------------

[root@sanchuang-linux chenpeng]# find . -name "*.sh" |xargs ls -al
-rw-r--r--. 1 root root 77 10月 16 15:30 ./redirect/abc.sh
-rw-r--r--. 1 root root 206 10月 16 15:45 ./redirect/student_information.sh
-rw-r--r--. 1 root root 300 10月 16 17:15 ./test.sh
[root@sanchuang-linux chenpeng]# ls -al test.sh
-rw-r--r--. 1 root root 300 10月 16 17:15 test.sh


相关文章

暂无评论

暂无评论...