假期作业60题

1.用sed修改1.txt的23行study为xxx;

sed -i '23 s/study/xxx/' 1.txt

2. 查看nginx日志50行第三列的内容

sed -n '50p' /usr/local/nginx/logs/access.log|awk '{print $3}'

3.删除日志文件里的空行

sed -i '/^$/d' /usr/local/nginx/logs/access.log

4.删除日志文件里中以#开头的行

sed -i '/^#/d' /var/log/httpd/access_log

5.写一个每天0点执行的删除nginx日志的脚本,保留最近七天的日志

脚本内容为:

#!/bin/bash
find /usr/local/nginx/logs/ -name "*.log" -ctime +7 -exec rm -f {
    
    } \;

定时任务编写如下:

[root@host-134 ~]# crontab -l
* 0 * * * /bin/bash /root/log.sh

6.查找名字为error.log的日志文件

find /var/log/  -name error.log

7.有一个文件,文件第二列为数字,查找第二列大于100的行?

文件内容为:

aaa 80
bbb 90
ccc 100
ddd 110
eee 120
fff 130
jjj 140
[root@host-134 ~]# awk '{if ($2>100) print}' 1.txt 
ddd 110
eee 120
fff 130
jjj 140

8.假如系统有100个系统账号,名字一次为name1-name100,编写脚本删除这些用户

脚本内容为:

#!/bin/bash
for i in `seq 1 100`
do
        userdel -r name$i
done

9.用iptable限制只有ip为192.168.0.55的IP访问本机22端口

iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 22 -j ACCEPT

10.查询文件里包含hhh的行并所在行的行号

[root@host-134 ~]# grep -n "hhh" 1.txt 
8:hhh
10:hhh

11.计算1到100相加

脚本内容为:

#!/bin/bash
sum=0
for i in `seq 1 100`
do
        sum=$[$i+$sum]
done
echo $sum

执行结果为:

[root@host-134 ~]# sh sum.sh 
5050

12.查找系统内文件大于60K小于100K的文件,并删除他们

find / -size +60k -a -size -100k -exec rm -f {
    
    } \;

13.统计nginx日志每个iP出现的次数

cat access.log |awk '{print $1}'|sort |uniq -c|sort -rnk1

14.统计log目录下以test开头的100个文件,然后把这100个文件的第一行保存到aaa这个文件中

脚本内容为:

#!/bin/bash
test_log=$(find /var/log -name "test*")
[ -f /root/aaa ] || touch /root/aaa
for i in ${test_log[*]}
do
        head -1 $i >> /root/aaa
done

执行结果为:

[root@host-134 ~]# sh log.sh 
[root@host-134 ~]# cat aaa
111
222
333
444
555

15. 1.txt 文件内容如下,打印数量超过三次的行

111 
aaa
111
222
333
111
cat 1.txt |sort |uniq -c|awk '{if ($1>3) print $2 }'

16.系统里如何查看某个包是否安装

rpm -qa|grep 要查询的包名

17.shell获取内存 CPU 硬盘

脚本内容为:

#!/bin/bash
mem_total=$(free -h | sed -n 2p |awk '{print $2}')
mem_used=$(free -h|sed -n 2p|awk '{print $3}')
cpu_cores=$(grep -c processor /proc/cpuinfo)
disk_total=$(lsblk |awk '/disk/{print $4}')
disk_used=$(df -h |grep '/$'|awk '{print $5}')
echo "内存总大小为:$mem_total"
echo "已使用内存大小为:$mem_used"
echo "CPU核心数为:$cpu_cores"
echo "磁盘总大小为:$disk_total"
echo "已用磁盘百分比为:$disk_used"

执行结果为:

[root@host-134 ~]# sh system.sh 
内存总大小为:1.8G
已使用内存大小为:110M
CPU核心数为:4
磁盘总大小为:40G
已用磁盘百分比为:5%

18.grep获取不包含an的行

grep -v "an" 1.txt

19.vim打开一个文件,替换文中所有的user为users

:%s/user/users

20.删除文件中最后一行

sed -i '$d' 1.txt

1.如何向脚本传递参数

执行脚本时直接在后面跟参数即可

sh test.sh 1 2

2.解释脚本中$@ $# $0 $? 含义

$@    以一个单字符串显示所有向脚本传递的参数
$#    传递到脚本的参数个数
$0     Shell本身的文件名
$?    上一条命令的退出状态。0表示没有错误,其他任何值表明有错误。

3.数据文件格式如下

test.txt文件内容为,要求输出第一个元素为aaa的行第二个字段

  aaa  bbb  ccc  ddd 
  Ccc  fff    mmm  nnn
  Ddd  aaa   sss   mmm
awk -e  '/^aaa/{print $2}' test.txt 
bbb

4.解释#!/bin/bash –xv 是啥意?

实现shell脚本逐条语句的跟踪并输出到屏幕
通常用作调试脚本

5.Shell脚本如何定义一个函数

定义函数的格式为:

function  函数名(   )
{
    
    
   函数体
}

函数名(   )
{
    
    
   函数体
}

6.Shell 中如何判断某个文件是否存在

操作符为:-f file

7.Shell 脚本中开始 #!/bin/bash 是啥意思

符号#!是一个约定的标记,告诉系统这个脚本需要什么解释器来执行

8.如何在后台运行脚本?Export命令有什么作用

在脚本后面添加 &

sh 1.sh &

在执行脚本之前加nohup更好,不挂断的运行脚本

nohup sh 1.sh &

export命令用于设置环境变量

用vim在/etc/profile文件中添加我们想要的环境变量
export 新环境变量名=内容

例:export BW=”wg007”
生效:source /etc/profile

9.Shell 脚本中单引号 双引号 反引号的区别

单引号:

单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的。
单引号字串中不能出现单独一个的单引号,必须成对出现。

双引号:

双引号里可以有变量
双引号里可以出现转义字符(\n,\t)

反引号:

反引号执行引号中的命令

10.列举shell中截取字符串的方法

cut  grep sed awk

11.写出输出数字 0 到 100 中 3 的倍数(0 3 6 9 …)的命令 ?

for i in {
    
    0..100..3}; do echo $i; done

12.[ $a == $b ] 和 [ $a -eq $b ] 有什么区别 ?列举其它的运算符。

== 用于比较字符串之间是否相等
-eq 用来比较数字之间是否相等
=	检测两个字符串是否相等,不相等返回 true。	
-z	检测字符串长度是否为0,为0返回 true。	
-n	检测字符串长度是否不为 0,不为 0 返回 true。
-eq	检测两个数是否相等,相等返回 true。	
-ne	检测两个数是否不相等,不相等返回 true。	
-gt	检测左边的数是否大于右边的,如果是,则返回 true。	
-lt	检测左边的数是否小于右边的,如果是,则返回 true。
-ge	检测左边的数是否大于等于右边的,如果是,则返回 true。	
-le	检测左边的数是否小于等于右边的,如果是,则返回 true。

13.比较两个数大小

脚本内容如下:

#!/bin/bash
if [ $1 -gt $2 ];then
        echo $1 大于 $2
elif [ $1 -eq $2 ];then
        echo $1 等于 $2
else
        echo $1 小于 $2
fi

执行结果:

[root@host-134 ~]# ./1.sh 1 2
1 小于 2
[root@host-134 ~]# ./1.sh 1 1
1 等于 1
[root@host-134 ~]# ./1.sh 2 1
2 大于 1

14.删除当前目录下大小为0的文件

find ./ -size 0 -exec rm -f {
    
    } \;

15.测试IP地址

ifconfig |awk '/broadcast/{print $2}'
192.168.153.135

用最简单化说明命令是干啥的,并列举三种最常用的方法

1.ifconfig 查看网卡的

例:关闭网卡 eth0

 ifconfig eth0 down

例:开启网卡 eth0

ifconfig eth0 up 

例:给网卡eth0配置ip地址

ifconfig eth0 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255

2.route 看路由的

例:查看路由表

[root@hdss7-12 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens32
10.4.7.0        0.0.0.0         255.255.255.0   U     100    0        0 ens32

例:添加默认网关

route -p add 0.0.0.0 mask 0.0.0.0 192.168.1.1

例:删除默认网关

route (-p) delete 0.0.0.0 mask 0.0.0.0 192.168.1.1

3.ping 测试网络通不通的

[root@host-134 ~]# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=47.8 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=47.6 ms

-c 参数可以指定ping的次数

[root@host-134 ~]# ping baidu.com -c 4
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=30.4 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=31.0 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=30.5 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=30.7 ms

4.traceroute 路由跟踪的

例:追踪本地数据包到www.baidu.com的传输路径:

[root@host-134 ~]# traceroute baidu.com
traceroute to baidu.com (39.156.69.79), 30 hops max, 60 byte packets
 1  gateway (192.168.153.2)  0.406 ms  0.224 ms  0.240 ms
 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  * * *
28  * * *
29  * * *
30  * * *

例:把对外发探测包的等待响应时间设置为3秒

traceroute baidu.com -w 3

例:跳数设置跳数为7

traceroute baidu.com -m 7

5.netstat 查看连接以及端口的

查看所有tcp和udp端口使用情况

[root@host-134 ~]# netstat -nltpu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1156/master         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1577/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1156/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1577/sshd           
udp        0      0 127.0.0.1:323           0.0.0.0:*                           762/chronyd         
udp6       0      0 ::1:323                 :::*                                762/chronyd         

列出所有端口

netstat -a

6.telnet 用来测试端口开没开的

查看主机22端口是否监听

[root@host-134 ~]# telnet 192.168.153.134 22
Trying 192.168.153.134...
Connected to 192.168.153.134.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4

查看主机3306端口是否监听

[root@host-134 ~]# telnet 192.168.153.134 3306
Trying 192.168.153.134...
telnet: connect to address 192.168.153.134: Connection refused

查看主机80端口是否监听

[root@host-134 ~]# telnet 192.168.153.134 80
Trying 192.168.153.134...
telnet: connect to address 192.168.153.134: Connection refused

7.rcp 远程跨主机拷贝文件的和SCP区别是不加密传输

使用rcp命令可以在远程主机之间复制文件,如果同时指定2个以上的文件或目录,且最后的目的地是一个已经存在的目录,则他会把前面指定的所有文件或目录复制到该目录中。

例:将135主机上的test目录下的子文件和子目录传输到135主机上的/root目录下

rcp -r /root/test  host-134:/root

例:将134主机上的134.txt文件传输到135主机上的/root目录下

rcp /root/134.txt host-135:/root

详情参考>> https://blog.csdn.net/m0_46674735/article/details/113768649

8.scp 远程跨主机拷贝文件的,加密传输

复制135主机/root/test/1.txt 的文件到当前目录下

scp 192.168.153.135:/root/test/1.txt .

复制135主机/root/test 目录到当前目录下

scp -r 192.168.153.135:/root/test .

9.top 实时显示系统中各个进程的资源占用状况

显示进程信息

top

以批处理模式显示程序信息

top -b

10.free 显示系统内存情况

free 
              total        used        free      shared  buff/cache   available
Mem:        1863104      324492      571312        9812      967300     1373744
free -m
              total        used        free      shared  buff/cache   available
Mem:           1819         316         557           9         944        1341
Swap:          2047           0        2047
free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        316M        558M        9.6M        944M        1.3G
Swap:          2.0G          0B        2.0G

11.vmstat 对操作系统的虚拟内存、进程、CPU活动进行监控

[root@host-134 ~]# vmstat 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1460172   2108 264648    0    0    20     5   19   21  0  0 100  0  0
[root@host-134 ~]# vmstat 5 2
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1460080   2108 264648    0    0    20     5   19   21  0  0 100  0  0
 0  0      0 1460056   2108 264648    0    0     0     0   16   19  0  0 100  0  0

第一个数字5,代表延迟5秒,后面的2代表刷新两次

12.iostat 监控磁盘io

命令的安装

yum install sysstat

查看CPU的状态,一秒刷新一次,共显示3次

[root@host-134 ~]# iostat -c 1 3
Linux 3.10.0-1062.el7.x86_64 (host-134) 	01/22/2021 	_x86_64_	(4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.08    0.00    0.15    0.17    0.00   99.60

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00

13.lsof 查看文件的进程信息

列出所有打开的文件

lsof

查看80端口的服务状态

[root@host-134 ~]# lsof -i :80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   14105   root    4u  IPv6  39250      0t0  TCP *:http (LISTEN)
httpd   14395 apache    4u  IPv6  39250      0t0  TCP *:http (LISTEN)
httpd   14396 apache    4u  IPv6  39250      0t0  TCP *:http (LISTEN)
httpd   14397 apache    4u  IPv6  39250      0t0  TCP *:http (LISTEN)
httpd   14398 apache    4u  IPv6  39250      0t0  TCP *:http (LISTEN)
httpd   14399 apache    4u  IPv6  39250      0t0  TCP *:http (LISTEN)

列出所有tcp 网络连接信息

lsof  -i tcp

列出所有udp 网络连接信息

lsof  -i udp

14.df 显示磁盘空间使用情况

显示磁盘分区使用情况

[root@host-134 ~]# df 
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   919496       0    919496   0% /dev
tmpfs                      931552       0    931552   0% /dev/shm
tmpfs                      931552    9820    921732   2% /run
tmpfs                      931552       0    931552   0% /sys/fs/cgroup
/dev/mapper/centos-root  38770180 1628732  37141448   5% /
/dev/sda1                 1038336  152744    885592  15% /boot
tmpfs                      186312       0    186312   0% /run/user/0

以容易阅读的方式显示磁盘分区使用情况

[root@host-134 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   37G  1.6G   36G   5% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0

-T 参数输出时显示文件系统类型

root@host-134 ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  898M     0  898M   0% /dev
tmpfs                   tmpfs     910M     0  910M   0% /dev/shm
tmpfs                   tmpfs     910M  9.6M  901M   2% /run
tmpfs                   tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        37G  1.6G   36G   5% /
/dev/sda1               xfs      1014M  150M  865M  15% /boot
tmpfs                   tmpfs     182M     0  182M   0% /run/user/0

15.du 对文件和目录磁盘使用的空间的查看

常用的两个参数

-h	以易读方式显示文件大小
-s	仅显示总计

查看/etc目录下所有文件和目录占用的大小

[root@host-134 ~]# du -hs /etc/
32M	/etc/

查看系统日志的大小

[root@host-134 ~]# du -h /var/log/messages 
664K	/var/log/messages

16.chown 改变文件或目录的属主和属组

修改1.txt文件的属主和属组为mysql

chown mysql:mysql /root/1.txt

修改 /var/www/html 目录下的子目录和文件属主和属组均为apache

chown -R apache:apache /var/www/html

17.chgrp 更改文件或目录的属组

修改文件的属组为root

chgrp root 1.txt

修改 /var/www/html 目录下的子目录和文件属组为root

chgrp -R root /var/www/html

18.chmod 修改文件或目录的权限

给脚本增加执行权限

chmod +x 1.sh

将/var/www/html目录下的所有文件与子目录权限设为777

chmod -R 777 /var/www/html

给系统日志增加所有人可读的权限

chmod a+r /var/log/messages

19.which 查找系统命令的位置

[root@host-134 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@host-134 ~]# which pwd
/usr/bin/pwd
[root@host-134 ~]# which cd
/usr/bin/cd

20.whereis 查找系统命令的位置

[root@host-134 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@host-134 ~]# whereis pwd
pwd: /usr/bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz
[root@host-134 ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz

21.locate 快速查找文件或目录

运行下面命令安装locate:

yum install mlocate

执行updatedb命令更新文件数据库

updatedb

查找和pwd相关的所有文件

[root@host-134 ~]# locate pwd
/etc/.pwd.lock
/usr/bin/pwd
/usr/bin/pwdx
/usr/include/pwd.h
/usr/lib/modules/3.10.0-1062.el7.x86_64/kernel/drivers/watchdog/hpwdt.ko.xz
/usr/lib64/cracklib_dict.pwd
/usr/lib64/python2.7/lib-dynload/spwdmodule.so
/usr/sbin/unix_chkpwd
/usr/share/cracklib/cracklib-small.pwd
/usr/share/cracklib/pw_dict.pwd
/usr/share/man/man1/pwd.1.gz
/usr/share/man/man1/pwdx.1.gz
/usr/share/man/man8/unix_chkpwd.8.gz

-c 只输出找到的数量

[root@host-134 ~]# locate ls -c
1289

22.find 查找和搜索文件或目录

搜索/etc 目录下大于10KB的文件

find /etc/ -type f -size +10k

搜索大于10G的日志文件,并删除

 find /var/log -type f -name "*.log" -size +10G -delete

查找 /home/ 下所有以a开头和以.txt结尾的文件

 find /home/ -name "*.txt" -a -name "a*"

23.cat 查看文件内容

查看 /etc/passwd 文件的内容

cat /etc/passwd

24 cat file 查看文件的内容

[root@hdss7-12 zuoye]# cat 2.txt 
111
222
333
aaa
bbb
ccc

25 cat -n file 查看文件的内容,并显示行号

[root@hdss7-12 zuoye]# cat -n 1.txt 
     1	aaa
     2	bbb
     3	ccc
     4	111
     5	222

26 more 分页显示文件内容

例:分页查看/var/log/messages文件的内容

more /var/log/messages

例:显示文件/var/log/messages的内容,每10行显示一次,而且在显示之前先清屏:

more -c -10 /var/log/messages

例:从第 20 行开始显示/var/log/messages 文档内容

more +20  /var/log/messages

27.less 分页显示工具

less的作用与more十分相似,不同点为less命令允许用户向前或向后浏览文件,而more命令只能向前浏览 。

例:分页查看/var/log/messages文件的内容

less /var/log/messages

例:ps查看进程信息并通过less分页显示:

ps -ef |less 

例:查看命令历史使用记录并通过less分页显示

history | less 

28.head 显示文件开头内容(默认输出前10行)

例:查看 /etc/passwd 文件前10行内容

head /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

例:查看 /var/log/messages 文件前5行内容

head -5 /var/log/messages
Feb  7 11:30:01 hdss7-12 systemd: Started Session 340 of user root.
Feb  7 11:40:01 hdss7-12 systemd: Started Session 341 of user root.
Feb  7 11:50:01 hdss7-12 systemd: Started Session 342 of user root.
Feb  7 12:00:01 hdss7-12 systemd: Started Session 343 of user root.
Feb  7 12:01:01 hdss7-12 systemd: Started Session 344 of user root.

29.ln 为文件创建链接

例:创建软链接

ln -s /etc/passwd /root/zuoye/passwd
[root@hdss7-12 zuoye]# ll
total 8
-rw-r--r-- 1 root root 20 Feb  7 17:12 1.txt
-rw-r--r-- 1 root root 24 Feb  7 17:12 2.txt
lrwxrwxrwx 1 root root 11 Feb  7 17:25 passwd -> /etc/passwd

例:创建硬链接

ln /var/log/messages /root/zuoye/messages
[root@hdss7-12 zuoye]# ll
total 32
-rw-r--r-- 1 root root    20 Feb  7 17:12 1.txt
-rw-r--r-- 1 root root    24 Feb  7 17:12 2.txt
-rw------- 2 root root 20492 Feb  7 17:26 messages
lrwxrwxrwx 1 root root    11 Feb  7 17:25 passwd -> /etc/passwd

30.diff 以逐行的方式,比较文本文件的异同处。

例:

diff 1.txt  2.txt 
0a1,3
> 111
> 222
> 333
4,5d6
< 111
< 222

例:加 -y参数可以并列的方式显示文件的异同之处

diff 1.txt  2.txt  -y
							      >	111
							      >	222
							      >	333
aaa								aaa
bbb								bbb
ccc								ccc
111							      <
222							      <

猜你喜欢

转载自blog.csdn.net/m0_46674735/article/details/113748549