shell题库选择题_Shell习题100例

每日一文件

https://github.com/aminglinux/shell100/blob/master/

要求:安照这样的日期格式(xxxx-xx-xx)每日生成一个文件,如生成的文件为2017-12-20.log,并且把磁盘的使用情况写到这个文件中,提示:date、df

[root@centos-04 tmp]# date

2018年 12月 26日 星期三 19:29:13 CST

[root@centos-04 tmp]# date +%Y

2018

[root@centos-04 tmp]# date +%y

18

[root@centos-04 tmp]# date +%d

26

[root@centos-04 tmp]# date +%m

12

[root@centos-04 tmp]# date +%H

19

[root@centos-04 tmp]# date +%M

30

[root@centos-04 tmp]# date +%S

52

[root@centos-04 tmp]# date +%s

1545823854

[root@centos-04 tmp]# date +%F

2018-12-26

[root@centos-04 tmp]# date +%T

19:31:04

[root@centos-04 tmp]#

[root@centos-04 tmp]# date +%w

3

[root@centos-04 tmp]# date +%W

52

[root@centos-04 tmp]#

昨天日期

[root@centos-04 tmp]# date -d "-1 day" +%F

2018-12-25

[root@centos-04 tmp]#

上一小时

[root@centos-04 tmp]# date -d "-1 hours" +%T

18:34:54

[root@centos-04 tmp]#

[root@centos-04 tmp]# vim 1.sh

#!/bin/bash

d=`date +%F`

df -h > $d.log

~

[root@centos-04 tmp]# sh 1.sh

[root@centos-04 tmp]# ls

1.sh ansible-ssh-192.168.242.130-22-root ansible-ssh-192.168.242.133-22-root lua_uwpzx3 tmp.SLBPtZ45L9

2018-12-26.log ansible-ssh-192.168.242.131-22-root elasticsearch.4Kw1U8qo nginx_proxy_tmp

456.log ansible-ssh-192.168.242.132-22-root hsperfdata_root proxy.log

[root@centos-04 tmp]#

[root@centos-04 tmp]# cat 2018-12-26.log

文件系统 容量 已用 可用 已用% 挂载点

/dev/mapper/centos-root 18G 6.2G 12G 36% /

devtmpfs 898M 0 898M 0% /dev

tmpfs 910M 0 910M 0% /dev/shm

tmpfs 910M 30M 881M 4% /run

tmpfs 910M 0 910M 0% /sys/fs/cgroup

/dev/sda1 497M 167M 331M 34% /boot

tmpfs 182M 0 182M 0% /run/user/0

overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged

shm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm

overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged

overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged

shm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm

shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm

[root@centos-04 tmp]#

改进版

d=`date +%F` #获取当前日期

dir=/data/logs/disklog  #指定日志文件生成的目录

if [ ! -d $dir ]  #判断如果没有目录创建

then

mkdir -p $dir

fi

df -h > $dir/$d.log  #将硬盘信息写到日志

find $dir/ -mtime +365 |xargs rm  #删除一年之前的文件

统计IP访问量

awk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r|less

[root@centos-04 tmp]# vim 2.sh

#!/bin/bash

awk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r

[root@centos-04 tmp]# sh 2.sh

统计内存占用之和

ps aux|sed '1d' (删除结果中第一行)

[root@centos-04 tmp]# vim 3.sh

#!/bin/bash

sum=0

for n in `ps aux |grep -v 'TIME COMMAND' |awk '{print $6}'`

do

sum=$[$sum+$n]

done

echo $sum

[root@centos-04 tmp]# sh 3.sh

114892

[root@centos-04 tmp]# free

total used free shared buff/cache available

Mem: 1863224 104260 1610364 9976 148600 1593352

Swap: 0 0 0

[root@centos-04 tmp]#

检测机器存活

[root@centos-04 tmp]# ping -c2 www.baidu.com|grep 'packet' |awk -F '%' '{print $1}' |awk '{print $NF}'

0

[root@centos-04 tmp]#

[root@centos-04 tmp]# vim mail.py

#!/usr/bin/env python

#-*- coding: UTF-8 -*-

import os,sys

reload(sys)

sys.setdefaultencoding('utf8')

import getopt

import smtplib

from email.MIMEText import MIMEText

from email.MIMEMultipart import MIMEMultipart

from subprocess import *

def sendqqmail(username,password,mailfrom,mailto,subject,content):

gserver = 'smtp.qq.com'

gport = 25

try:

# msg = MIMEText(unicode(content).encode('utf-8')) //如果发送的邮件有乱码,可以尝试把这行改成如下:

msg = MIMEText(content,'plan','utf-8')

msg['from'] = mailfrom

msg['to'] = mailto

msg['Reply-To'] = mailfrom

msg['Subject'] = subject

smtp = smtplib.SMTP(gserver, gport)

smtp.set_debuglevel(0)

smtp.ehlo()

smtp.login(username,password)

smtp.sendmail(mailfrom, mailto, msg.as_string())

smtp.close()

except Exception,err:

print "Send mail failed. Error: %s" % err

def main():

to=sys.argv[1]

subject=sys.argv[2]

content=sys.argv[3]

##定义QQ邮箱的账号和密码,你需要修改成你自己的账号和密码(请不要把真实的用户名和密码放到网上公开,否则你会死的很惨)

sendqqmail('1234567@qq.com','aaaaaaaaaa','1234567@qq.com',to,subject,content)

if __name__ == "__main__":

main()

#####脚本使用说明######

#1. 首先定义好脚本中的邮箱账号和密码

#2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"

[root@centos-04 tmp]# vim 4.sh

#!/bin/bash

n=`ping -c5 www.baidu.com|grep 'packet' |awk -F '%' '{print $1}' |awk '{print $F

N}'`

if [ -z "$n" ]

then

echo "脚本有问题。"

exit

else

n1=`echo $n|sed 's/[0-9]//g'`

if [ -n "$n" ]

then

echo "脚本$0有问题。"

exit

fi

fi

m=123@qq.com

while :

do

if [ $n -ge 50 ]

then

python mail.py $m "机器宕机" "丢包率$n%"

fi

sleep 30

done

批量改文件名

[root@centos-04 tmp]# cp -r /123/ /123.bak

[root@centos-04 tmp]# tar -tf 123.tar.gz

[root@centos-04 tmp]# vim 5.sh

#!/bin/bash

find /123/ -type f -name "*.txt" > /tmp/txt.list

for f in `cat /tmp/txt.list`

do

mv $f $f.bak

done

#find /123/ -type f -name *.txt |xargs -i mv {} {}.bak

#find /123/ -type f -name *.txt -exec mv {} {}.bak \;

for f in `cat /tmp/txt.list`

do

echo $f.bak

done > /tmp/txt.bak.list

tar -czvf 123.tar.gz `cat /tmp/txt.bak.list |xargs`

for f in `cat /tmp/txt.list`

do

mv $f.bak $f

done

[root@centos-04 tmp]# rsync -av /123.bak/ /123/

检测80端口

[root@centos-04 tmp]# netstat -lntp |grep 80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6938/nginx: master

tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 6938/nginx: master

tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 6938/nginx: master

[root@centos-04 tmp]# netstat -lntp |grep ':80 '

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6938/nginx: master

[root@centos-04 tmp]#

[root@centos-04 tmp]# yum install -y nmap

[root@centos-04 tmp]# nmap -p 80 127.0.0.1

Starting Nmap 6.40 ( http://nmap.org ) at 2019-01-04 03:29 CST

Nmap scan report for localhost (127.0.0.1)

Host is up (0.00011s latency).

PORT STATE SERVICE

80/tcp open http

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds

[root@centos-04 tmp]#

[root@centos-04 tmp]# vim 6.sh

#!/bin/bash

m=123@123.com

while :

do

n=`netstat -lntp |grep ':80 '|wc -l`

if [ $n -eq 0 ]

then

/usr/local/apache2/bin/apachectl -k restart 2>/tmp/apache.err

python mail.py $m "80端口关闭" "已经重启httpd服务"

pn=`pgrep -l httpd|wc -l`

if [ $pn -eq 0 ]

then

python mail.py $m "httpd重启失败" "`head -1 /tmp/apache.err`"

fi

sleep 30

done

也可以用nohup或screen这screen里执行6.sh

[root@centos-04 tmp]# nohup sh 6.sh &

[root@centos-04 tmp]# nmap -p 80 127.0.0.1 |grep '80/tcp' |awk '{print $2}' (用nmap写法)

open

[root@centos-04 tmp]#

备份数据库

[root@centos-04 tmp]# vim 7.sh

#!/bin/bash

d1=`date +%w`

d2=`date +%d`

local_backdir=/bak/mysql

remote_backdir=192.168.242.130::backup

exec 1> /tmp/mysqlbak.log 2>/tmp/mysqlbak.err

echo "mysql backup begin at `date`"

mysqldump -uroot -pxxx discuz > $local_backdir/discuz.sql.$d1

rsync -az $local_backdir/discuz.sql.$d1 $remote_backdir/discuz.sql.$d2

echo "mysql backup end at `date`"

检测502

1.502的情况:php配置有问题、php脚本耗资源。

[root@centos-04 tmp]# vim 8.sh

#!/bin/bash

log=/data/log/access.log

while :

do

502_n=`tail -n 300 $log |grep -c ' 502 '`

if [ -z "$502_n" ]

then

exit

fi

if [ $502_n -gt 100 ]

then

/etc/init.d/php-fpm restart >/dev/null 2>/tmp/php-fpm.err

fpm_p_n=`pgrep -l php-fpm|wc -l`

if [ $fpm_p_n -eq 0 ]

then

python mail.py xxx@xx.com "php-fpm err" "head -1 /tmp/php-fpm.err"

exit

fi

fi

sleep 10

done

删除字母和行

1.删除前五行中包含字母的行

[root@centos-04 tmp]# head -n5 test.sql |sed '/[a-zA-Z]/d'

2.查看文件前5行

[root@centos-04 tmp]# sed -n '1,5'p mail.py

#!/usr/bin/env python

#-*- coding: UTF-8 -*-

import os,sys

reload(sys)

sys.setdefaultencoding('utf8')

[root@centos-04 tmp]#

3.把6到10行中的全部字母删掉

[root@centos-04 tmp]# vim 9.sh

#!/bin/bash

sed -n '1,5'p 1.txt|sed '/[a-zA-Z]/d'

sed '1,5d' 1.txt |sed '1,5s/[a-zA-Z]//g'

找单词

[root@centos-04 tmp]# for w in Bash also interprets a number of multi-character options.; do echo $w; done

Bash

also

interprets

a

number

of

multi-character

options.

[root@centos-04 tmp]#

[root@centos-04 tmp]# vim 10.sh

#!/bin/bash

c="Bash also interprets a number of multi-character options."

n=`echo $c|awk -F '[ +-.]' '{print NF}'`

for ((i=1;i

do

l=`echo $c|awk -F '[ +-.]' -v j=$i '{print $j}'|wc -L`

if [ $l -lt 6 ]

then

echo $c|awk -F '[ +-.]' -v j=$i '{print $j}'

fi

done

[root@centos-04 tmp]# sh 10.sh

Bash

also

a

of

multi

[root@centos-04 tmp]#

输入数字执行命令

[root@centos-04 tmp]# vim 11.sh

#!/bin/bash

echo "*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd"

read -p "Please input a number:" n

if [ -z "$n" ]

then

echo "请输入一纯数字,范围1-4。"

exit

fi

n1=`echo $n|sed 's/[0-9]//g'`

if [ -n "$n1" ]

then

echo "请输入一个纯数字,范围1-4。"

exit

fi

case $n in

1)

date

;;

2)

ls

;;

3)

who

;;

4)

pwd

;;

*)

echo "请输入1-4的数字"

;;

esac

[root@centos-04 tmp]# sh 11.sh

*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd

Please input a number:1

2019年 01月 04日 星期五 21:43:35 CST

[root@centos-04 tmp]# sh 11.sh

*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd

Please input a number:2

10.sh 11.sh 1.sh 2018-12-26.log 2.sh 3.sh 456.log 4.sh 5.sh 6.sh 7.sh 8.sh 9.sh mail.py test.sql tmp.SzNhh17qiE

[root@centos-04 tmp]# sh 11.sh

*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd

Please input a number:3

root pts/1 2019-01-04 18:26 (192.168.242.1)

[root@centos-04 tmp]# sh 11.sh

*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd

Please input a number:4

/tmp

[root@centos-04 tmp]# sh 11.sh

*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd

Please input a number:5

请输入1-4的数字

[root@centos-04 tmp]# sh 11.sh

*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd

Please input a number:fsaf

请输入一个纯数字,范围1-4。

[root@centos-04 tmp]#

批量创建用户

[root@centos-04 tmp]# seq -w 00 09

00

01

02

03

04

05

06

07

08

09

[root@centos-04 tmp]# seq 0 9

0

1

2

3

4

5

6

7

8

9

[root@centos-04 tmp]#

给用户添加密码

[root@centos-04 tmp]# useradd user1

[root@centos-04 tmp]# passwd user1

更改用户 user1 的密码 。

新的 密码:

无效的密码: 密码少于 7 个字符

重新输入新的 密码:

passwd:所有的身份验证令牌已经成功更新。

[root@centos-04 tmp]#

在脚本中自动给用户添加密码两种方式

[root@centos-04 tmp]# echo -e "user1\nuser1\n" |passwd user1

更改用户 user1 的密码 。

新的 密码:无效的密码: 密码少于 7 个字符

重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。

[root@centos-04 tmp]# echo "user1" |passwd --stdin user1

更改用户 user1 的密码 。

passwd:所有的身份验证令牌已经成功更新。

[root@centos-04 tmp]#

[root@centos-04 tmp]# vim 12.sh

#!/bin/bash

for i in `seq -w 00 09`

do

useradd user_$i

p=`mkpasswd -l 10 -s 0`

echo "user_$i $p" >> /tmp/pass.tmp

echo $p |passwd --stdin user_$i

done

[root@centos-04 tmp]# sh 12.sh

更改用户 user_00 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_01 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_02 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_03 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_04 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_05 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_06 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_07 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_08 的密码 。

passwd:所有的身份验证令牌已经成功更新。

更改用户 user_09 的密码 。

passwd:所有的身份验证令牌已经成功更新。

[root@centos-04 tmp]#

[root@centos-04 tmp]# tail /etc/passwd

user_00:x:1001:1001::/home/user_00:/bin/bash

user_01:x:1002:1002::/home/user_01:/bin/bash

user_02:x:1003:1003::/home/user_02:/bin/bash

user_03:x:1004:1004::/home/user_03:/bin/bash

user_04:x:1005:1005::/home/user_04:/bin/bash

user_05:x:1006:1006::/home/user_05:/bin/bash

user_06:x:1007:1007::/home/user_06:/bin/bash

user_07:x:1008:1008::/home/user_07:/bin/bash

user_08:x:1009:1009::/home/user_08:/bin/bash

user_09:x:1010:1010::/home/user_09:/bin/bash

[root@centos-04 tmp]# cat /tmp/pass.tmp

user_00 8xiwgZSce6

user_01 yaMp6cb2gA

user_02 jx0QtlL2fw

user_03 69bwuqgEDf

user_04 p3fpvMMl9c

user_05 fm5Bv4Xssx

user_06 ivx69zVIpy

user_07 l77CvxvuHy

user_08 MZfmi6kx4f

user_09 4bAkzeaKa6

[root@centos-04 tmp]#

登录测试(复制user_00的密码粘贴密码登录成功)

[root@centos-04 ~]# ssh user_00@192.168.242.130

user_00@192.168.242.130's password:

Last login: Sat Jan 5 00:00:28 2019

[user_00@centos-04 ~]$

删掉刚刚创建的用户

[root@centos-04 tmp]# for i in `seq -w 00 09`;do userdel -r user_$i; done

[root@centos-04 tmp]# tail /etc/passwd

tcpdump:x:72:72::/:/sbin/nologin

rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin

rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

haproxy:x:188:188:haproxy:/var/lib/haproxy:/sbin/nologin

dockerroot:x:994:991:Docker User:/var/lib/docker:/sbin/nologin

epmd:x:993:990:Erlang Port Mapper Daemon:/tmp:/sbin/nologin

rabbitmq:x:992:989:RabbitMQ messaging server:/var/lib/rabbitmq:/bin/bash

user1:x:1000:1000::/home/user1:/bin/bash

监控httpd进程

[root@centos-04 tmp]# vim 13.sh

#!/bin/bash

check_service()

{

c=0

for i in `seq 1 5`

do

/usr/local/apache2/bin/apachectl -k restart 2> /tmp/httpd.err

if [ ! $? -eq 0 ]

then

c=$[$c+1]

else

break

fi

done

if [ $c -eq 5 ]

then

python mail.py "123@qq.com " "apache进程数量大于500,重启失败。" "`head -1 /tmp/httpd.err`"

exit

fi

}

while :

do

n=`ps -C httpd --no-heading|wc -l`

if [ $n -ge 500 ]

then

check_service

sleep 60

n_new=`ps -C httpd --no-heading|wc -l`

if [ $n_new -ge 500 ]

then

python mail.py "123@qq.com " "apache重启一分钟后进程数量仍然大于500" "请登录服务器排查问题"

exit

fi

fi

sleep 10

done

封ip

[root@centos-04 logs]# tail access.log

127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/replicationcontrollers?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/

scheduler"

127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/services?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"

127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/apps/v1beta1/statefulsets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/

scheduler"

127.0.0.1 - - [19/Dec/20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值