09_Linux基础-SHELL-标准输入与标准输出-重定向-管道
博客🔗:https://blog.csdn.net/cpen_web
作业
编辑文本文件/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 标准输入和标准输出
标准输入(stdin)和标准输出(stdout)
进程:正在进行的程序
https://blog.csdn.net/xiaofei0859/article/details/78448493?utm_source=blogxgwz9
每个进程基本都会打开三个文件描述符
0:标准输入 (从键盘、语音、人脸识别……)
1:标准输出 (输出到屏幕)
2:标准错误输入(输出到屏幕)
[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
......
虚拟终端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虚拟终端
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
roottty1
– 0610月20 1:32m 0.60s 0.60s -bash(注:bash环境)
rootpts/1
192.168.0.26 10:03 25:40 0.21s 0.21s -bash(注:bash环境)
rootpts/2
10.122.155.164 08:52 2:29m 0.16s 0.13s vim -r a(注:删除环境)
rootpts/6
192.168.0.26 10:40 4.00s 0.09s 0.01s w(注:输入w命令环境)
[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机器的平均负载
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
roottty1
– 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
roottty2
– 11:40 1:50 0.01s 0.01s -bash
roottty6
– 11:41 13.00s 0.01s 0.01s -bash
如何拦截虚拟终端
知识点1.6 如何拦截虚拟终端
如何拦截虚拟终端 #kill -9 pid
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
/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]# ls0 1 2 255
[root@mysql-binary fd]# pwd/proc/1680/fd
类型 操作符 用途
重定向标准 输出
> 将命令的执行结果 输出到指定的文件
中,而不是直接显示在屏幕上
\>> 将命令执行的结果追加输出到指定文件
重定向标准 输入
< 将命令中 接收输入的途径
由默认的键盘 更改 为指定的文件
### echo
> **知识点3.1 echo** echo -n 不接换行
echo -e 转义字符输出
bash
[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]#
注↑:标注错误输出 、正常输出
### 重定向
> **知识点3 重定向**
重定向:改变标准输入、标准输出的方向就是重定向
操作如下
**知识点3.4 # sh test.sh > test_result.txt**
标准错误输出不会重定向到文件,正确输出会重定向到文件
[root@sanchuang-linux redirect]# vim test.sh
[root@sanchuang-linux redirect]# cat test.sh<br>
<br>
[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 重定向 标准错误**
标准错误:通过该设备报告执行出错信息
操作如下
**知识点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<br>
2020年 10月 16日 星期五 14:52:33 CST (注:正确输出显示在屏幕上)
[root@sanchuang-linux redirect]# cat test_result
.txt<br>
.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
>stdout_test.txt (注:有空格)
"">stdout_test.txt (注:有空格)
:>stdout_test.txt
-n "" > stdout_test.txt
代码如下
[root@sanchuang-linux redirect]# echo
"aaa" > stdout_test.txt
[root@sanchuang-linux redirect]# cat stdout_test.txt<br>
[root@sanchuang-linux redirect]# echo
"bb" >> stdout_test.txt
[root@sanchuang-linux redirect]# cat stdout_test.txt<br>
<br>
[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 接受从键盘的输入 管道符号
脚本 接受从键盘的输入
要求:输入用户名和密码 创建相应的用户
需求分析:接受用户输入
创建用户
设置密码
[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**
bash
[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)
</code></pre>
<blockquote>
<p><strong>4.3.4 脚本改善 最终版</strong>
[root@sanchuang-linux chenpeng]# <code>id chen5557</code>
uid=1019(chen5557) gid=1019(chen5557) 组=1019(chen5557)</p>
</blockquote>
<pre><code>
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命令
Original: https://www.cnblogs.com/mycpen/p/16650962.html
Author: mycpen
Title: 09_Linux基础-SHELL-标准输入与标准输出-重定向-管道
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/578016/
转载文章受原作者版权保护。转载请注明原作者出处!