设为首页 收藏本站
查看: 1630|回复: 0

shell 脚本之基础篇作业

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-16 09:43:02 | 显示全部楼层 |阅读模式
1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版   本,内核版本,CPU型号,内存大小,硬盘大小。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
echo "The hosname is: $(hostname)"
echo "The kernel version is: $(uname -r)"
echo "The server_ip is: $( ifconfig |grep 'inet\b' |grep -v '127.0.0.1' |sed 's/.*addr:\b//' |sed 's/Bcast.*//')"
echo "The CPU is: $(lscpu |grep -i 'model name' |tr -s ' ' |sed 's/.*://')"
echo "The OS version is: $(cat /etc/redhat-release)"
echo "The memorysize is: $(free |sed -n '2p' |tr : ' ' |tr -s ' ' |cut -d' ' -f2)"
echo "The disksize is: $(fdisk -l |sed -n '2p')"
[iyunv@localhost bin]# bash -n systeminfometion.sh
[iyunv@localhost bin]# bash systeminfometion.sh
The hosname is: localhost.localdomain
The kernel version is: 2.6.32-642.el6.x86_64
The server_ip is: 10.1.253.35  
The CPU is:  Intel(R) Core(TM) i3-2350M CPU @ 2.30GHz
The OS version is: CentOS release 6.8 (Final)
The memorysize is: 1004108
The disksize is: Disk /dev/sda: 128.8 GB, 128849018880 bytes
[iyunv@localhost bin]# bash -n systeminfometion.sh
[iyunv@localhost bin]# vim systeminfometion.sh
You have new mail in /var/spool/mail/root



2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中



1
2
3
4
5
6
backdir="/root/etc$(date +%F)"
echo $backdir
cp -a /etc/. $backdir && echo "backup finished"
unset backdir
[iyunv@localhost ~]# ll
drwxr-xr-x. 75 root root 4096 Aug 12 05:11 etc2016-08-11



3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  显示当前硬盘分区中空间利用率最大的值



1
2
3
disk_max=$(df |tr -s ' ' % |cut -d% -f5 |grep '[0-9]' |sort -nr |head -1)
echo "The diskused_max is $disk_max"
unset disk_max



1
2
[iyunv@localhost bin]# bash disk.sh
The diskused_max is 53



4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数   从大到小排序
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数   从大到小排序



1
2
3
4
5
netstat -nt |tr -s ' ' |cut -d' ' -f5 |grep '[0-9]' |sed 's/:.*//' |sort |uniq -c |sort
[iyunv@localhost bin]# bash link.sh
1 10.10.10.1
1 10.1.250.69
3 10.1.50.10



5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  计算/etc/passwd文件中的第10个用户和第20用户的ID之和
user10_id=$(sed -n '10p' /etc/passwd |cut -d: -f3)
user20_id=$(sed -n '20p' /etc/passwd |cut -d: -f3)
sumid=$[$user10_id+$user20_id]
echo "The sum_id is: $sumid"
unset user10_id
unset user20_id
unset sumid



6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空   白行之和
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和



1
2
3
4
5
6
7
8
file1=$(grep '^$' $1 |wc -l)
file2=$(grep '^$' $2 |wc -l)
sumspace=$[$file1+$file2]
echo "The total spacelines is $sumspace"
unset file1
unset file2
[iyunv@localhost bin]# bash sumspace.sh  /etc/fstab /etc/profile
The total spacelines is 12




6、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   统计/etc, /var, /usr目录中共有多少个一级子目录和文件



1
2
3
4
5
6
7
8
9
10
etc_sum=$(ls -A /etc |wc -l)
var_sum=$(ls -A /var |wc -l)
usr_sum=$(ls -A /usr |wc -l)
sumfile=$[$etc_sum+$var_sum+$usr_sum]
echo "The total file is $sumfile"
unset etc_sum
unset var_sum
unset usr_sum
[iyunv@localhost bin]# bash sumfile.sh
The total file is 208



7、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用     户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件   中的空白行数
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数



1
2
3
[ $# -lt 1 ] && echo "please give a argument" || grep '^$' $1 |wc -l
[iyunv@localhost bin]# bash argsnum.sh /etc/fstab
1



8、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能   ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”



1
2
3
4
5
[ $# -ge 1 ] && echo "$1" |egrep -o '(\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))' &> /dev/null && (ping -c 3 $1 &>/dev/null && echo -e "\e[31m this ip is accessed\e[0m" || echo -e "\e[31m this ip is not accessed \e[0m")
[iyunv@localhost bin]# bash hostping.sh 10.1.1.1
this ip is not accessed
[iyunv@localhost bin]# bash hostping.sh 10.1.253.35
this ip is accessed



9、chmod -rw /tmp/file1,编写脚本/root/bin/per.sh,判断当前用户对/tmp/fiile1文件是否不可读且   不可写
1
2
3
[iyunv@localhost tmp]# chmod -rw file1
[iyunv@localhost tmp]# ll file1
----------. 1 user1 user1 0 Aug 12 08:10 file1



1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:    判断当前用户对/tmp/fiile1文件是否不可读且不可写



1
2
3
[ -r /tmp/file1 -a -w /tmp/file1 ] && echo "Yous can not to read and writ file1"
[iyunv@localhost bin]# bash per.sh
Yous can not to read and writ file1



10、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统。
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   实现禁止和充许普通用户登录系统



1
2
3
[ -f /etc/nologin ] && (rm -f /etc/nologin;echo user enable login) || echo user disable login already
[iyunv@localhost bin]# bash login.sh
user disable login already



11、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,先判断是否合格IP,否,   提示IP格式不合法并退出,是,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;   如果不可ping通,则提示用户“该IP地址不可访问”
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 接受一个主机的IPv4地址做为参数,先判断是否合格IP,否,提示IP格式不合法并退出,是,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;   如果不可ping通,则提示用户“该IP地址不可访问
#!/bin/bash
[ $# -ge 1 ] && echo "$1" |egrep -o '(\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))' &> /dev/null && (ping -c 3 $1 &>/dev/null && echo -e "\e[31m this ip is accessed\e[0m" || echo -e "\e[31m this ip is not accessed \e[0m")
[iyunv@localhost bin]# bash hostping.sh 10.1.1.1
this ip is not accessed
[iyunv@localhost bin]# bash hostping.sh 10.1.253.35
this ip is accessed



12、计算1+2+3+...+100的值
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   计算1+2+3+...+100的值



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
echo {1..10} |tr ' ' + |bc
[iyunv@localhost bin]# bash sum1.sh
5050

i=1
sum=0
for i in {1..100};do
    sum=$[$sum+$i]
    i=$i+1
done
echo "the sum is $sum"
unset i
unset sum
[iyunv@localhost bin]# bash sum.sh
the sum is 5050




13、计算从脚本第一参数A开始,到第二个参数B的所有数字的总和,判断B是否大于A,否提示错误并退出,是则计算之
1
2
3
4
5
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  计算从脚本第一参数A开始,到第二个参数B的所有数字的总和,判断B是否大于A,否提示错误并退出,是则计算之



1
2
3
4
5
6
#!/bin/bash
[[ $1 -lt $2 ]] && (echo $(seq $1 $2) |tr ' ' + |bc) || echo "error,please input effective number"
[iyunv@localhost bin]# bash f1.sh 10 30
420
[iyunv@localhost bin]# bash f1.sh 100 30
error,please input effective number






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-258593-1-1.html 上篇帖子: 简单shell脚本编程示例 下篇帖子: shell脚本中if与case使用,查找文件locate与find的使用,压缩...
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表