shell脚本

最近学了shell脚本,自己感觉挺有难度的,今天就简单整理一些shell脚本的练习和作业

练习:

   1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。
#!/bin/bash
# ——————————————————–
# Filename: 1.sh                                        
# Revision: 1.0                                          
# Description: Centos7配置信息                     
# ——————————————————–
ipv4=`ifconfig ens33 |grep “\<inet\>”|tr -s ‘ ‘|cut -d ‘ ‘ -f3` 
banben=`cat /etc/redhat-release;uname -r`
CPU=`lscpu|grep “Model name”|cut -d : -f 2|tr -s ‘ ‘`
Ferr=$[$(cat /proc/meminfo |head -1|grep -o “[[:digit:]]\+”)/1024/1024]
Fdisk=`fdisk -l|head -n 2|tail -n 1|cut -d ‘ ‘ -f3,4`
echo “系统基本信息”
echo “name:”$(hostname)
echo “IPV4地址:””$ipv4”
echo “版本信息和内核:”$banben
echo “CPU型号:””$CPU”
echo “内存大小:””$Ferr GB”
echo “硬盘大小:””$Fdisk”

2、编写脚本/root/bin/backup.sh,可实现将/etc/目录备份到/root/etcYYYY-mm-dd中 
#!/bin/bash
cp -a /etc /root/etc$(date +%F)

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
#——————————————–
#Filename: 
#Revision: 1.0
#Date:
#Description:硬盘利用率最大的值:
#——————————————–
echo “硬盘利用率最大的值:$(df |grep -o “\<[[:digit:]]\{1,3\}%” |sort -n |tail -n 1)”;

4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
#!/bin/bash
netstat -tun | grep “[0-9]” | tr -s ‘ ‘ : |cut -d: -f6|sort|uniq -c|sort -rn

1.写一个脚本名为jiaozuoyexx.sh 当执行该脚本时如jiaozuoyeXX.sh testXX.sh,就会自动将该testXX.sh传给教师机,路径是
scp testXX.sh mage26@172.17.252.213:~/scripts  密码为mage26

#!/bin/bash
# ——————————————
# Filename:  jiaozuoye16.sh
# Revision:  1.0 
# Date:    
# Author:  
# Description: 交作业
# ——————————————

scp $1 mage26@172.17.252.213:~/scripts

2.写一个能够创建新脚本的Shell script,如名为createshXX.sh 当执行时createsh /root/bin/test1.sh
则会自动创建并打开/root/bin/test1.sh,且其中包含以下内容。
#!/bin/bash
# ——————————————
# Filename: 
# Revision: 
# Date: 
# Author: 
# Email:
# Website:
# Description: 
# ——————————————

—————————–答案1————————
#!/bin/bash
# ———————–
# filename:
# revision:
# date:
# author:
# email:
# website:
# description:
# ———————–
echo “#!/bin/bash
# ———————–
# filename:
# revision:
# date:
# author:
# email:
# website:
# description:
# ———————–
” >>$1
chmod +x $1
vim $1

———————答案2—————————————

#!/bin/bash
touch $1
cat >$1<<EOF
#!/bin/bash
# ——————————————
# Filename: 
# Revision: 
# Date: 
# Author: 
# Email:
# Website:
# Description: 
# ——————————————
#”***********************脚本内容如下*********************”
EOF

chmod +x $1
vi   $1

—————————答案3———————–
#!/bin/bash
#———————————————————-
#Filename:moban
#Revision:1.0
#Description:
#Date:2017年8月2日18:14:59
#———————————————————-
#Filename:moban
#Revision:1.0
#Description:
#Date:2017年8月2日18:14:59
#Email:dreamworks.cnn@gmail.com
#uthor:THINKLXY.50
#Website:
#———————————————————-
read -p “Enter the ScriptName: ” name
touch $name

A=(0 1 2 3 4 5 6 7 8 9 10)

A[1]=”#!/bin/bash\n”
A[2]=”#———————————————————-\n”
A[3]=”#Filename:\n”
A[4]=”#Revision(版本):1.0\n”
A[5]=”#Description:\n”
A[6]=”#Date:\n”
A[7]=”#Email:dreamworks.cnn@gmail.com\n”
A[8]=”#Website(博客):\n”
A[9]=”#Author(作者):THINKLXY\n”
A[10]=”#———————————————————-\n”
A[0]=

echo -e “${A[*]}” |sed “s/^[[:blank:]]//” >> $name
chmod 700 $name

vim $name

1、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和 
#!/bin/bash
# —————————————————-
# Filename: sumid.sh                                               
                                 
# Revision: 1.0
# Date: 2017/08/02
# Author: along
# Description: 计算/etc/passwd 第10和20用户ID之和
# —————————————————-
root1=$(cat /etc/passwd | head -$1 |tail -1 | cut -d: -f3)
root2=$(cat /etc/passwd | head -$2 |tail -1 | cut -d: -f3)
echo $[$root1+$root2]
unset root1 root2

补充:  cat -n和nl都是显示行号的

对比自己,还有很多不足,需要改进

shell脚本                   


2、编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和 

#!/bin/bash
# ——————————————
# Filename: sumspace04.sh 
# Revision: null
# Date: 2017-08-04 06:32:47
# Author: zhaodong
# Email: 1213217229@qq.com
# Website: null
# Description: new file
# ——————————————
read -p “please input first filename:” first_filename
read -p “please input second filename:” second_filename
file1=`cat $first_filename|grep ^[[:space:]]*$|wc -l`
file2=`cat $second_filename|grep ^[[:space:]]*$|wc -l`
echo “sum=$[$file1+$file2]”

———————————–答案————————————–

#!/bin/bash
# ——————————————
# Filename:sumspace.sh
# Revision:
# Date:2017-8-3
# Author:lily Lee
# Email:2319761707@qq.com
# Website:
# Description:This is script
# ——————————————
_space=`cat $@ | egrep “^[[:space:]]*$” |wc -l`
echo $_space

3、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件

#!/bin/bash
# ——————————————
# Filename:sumspace.sh
# Revision:
# Date:2017-8-3
# Author:lili Lee
# Email:2319761707@qq.com
# Website:
# Description:This is a script
# ——————————————
_etc=`ls -A $1 | wc -l`
_var=`ls -A $2 | wc -l`
_usr=`ls -A $3 | wc -l`
let sum=$_etc+$_var+$_usr
echo $sum

对比自己

shell脚本
   
1、编写脚本/root/bin/argsnumsh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

[   “$@”  ]&&(cat $1 |grep “^[[:space:]]*$” |wc -l) ||(echo 至少应该给一个文件参数!;exit)

2、编写脚本/root/bin/hostping.h,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

ping -c1 -w1 $1 &> /dev/null && echo “该IP地址可以访问!”||echo”该IP地址不可访问!”

3、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
diskused_max=df | grep sd|sort -nr -k5|head -1|tr -s ' ' %|cut -d% -f5
inodeused_max=df -i| grep sd|sort -nr -k5|head -1|tr -s ' ' %|cut -d% -f5
[ “$diskused_max” -gt “80” ] && wall “空间即将满”||echo “空间使用率不超过80%”
[ “$inodeused_max” -gt “80” ] && wall “inode即将满”||echo “inode使用率不超过80%”
unset diskused_max inodeused_max 

———————————————-答案二——————————————————–
dev=df|grep "/dev/sd"|egrep -o "[0-9]{1,3}%"|sort -n|tail -n 1|cut -d% -f1
ino=df -i|egrep -o "[0-9]{1,3}%" |sort -n|tail -n 1|cut -d% -f1
[[ “$dev” -gt 80 ]] || [[ “$ino” -gt 80 ]] && echo $(wall 磁盘已满)
unset dev
unset ino
    

作业:
1..编写脚本/bin/per.sh,判断当前用户对指定的参数文件,是否不可读并且不可写
#!/bin/bash
# ——————————————
# Filename:
# Revision:
# Date:
# Author:
# Email:
# Website:
# Description:
# ——————————————
# read -p “please input filename:”
$1

[ ! -r $1 -a ! -w $1 ] && echo true || echo false

用到如下知识点

shell脚本

2.编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
#!/bin/bash
# ——————————————
# Filename:
# Revision:
# Date:
# Author:
# Email:
# Website:
# Description:
# ——————————————
# read -p “please input a filename:” $1
#[ -f $1 ] && [[ $1 =~ “.sh\>” ]] && chmod u+x $1 || echo “这是一个非脚本文件”
ls $1 |grep -q “.sh$” && [ -f $1 ] && chmod u+x $1 && ls -l $1 || echo “这是一个非脚本文件”
3.编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
#!/bin/bash
# ——————————————
# Filename:
# Revision:
# Date:
# Author:
# Email:
# Website:
# Description:
# ——————————————
#id $1 |cut -d ” ” -f1
#id $1 |tr -s ‘ ‘ |cut -d “=” -f2 |cut -d ” ” -f1 && [ $1 -eq 1000 ] && echo “禁止非普通用户登录”
id $1 |tr -s ‘ ‘ |cut -d “=” -f2 |cut -d ” ” -f1 |grep -q -o “[0-9]\{4,\}”&& usermod -L $1 && echo “禁止登录”
#!/bin/bash
# ——————————————
# Filename:
# Revision:
# Date:
# Author:
# Email:
# Website:
# Description:
# —————————————–

id $1 |tr -s ‘ ‘ |cut -d “=” -f2 |cut -d ” ” -f1 |grep -q -o “[0-9]\{4,\}” && usermod -U $1 && echo “please input name and passwd”

shell脚本

补充:这里简单介绍一个特殊的命令

shell脚本

vim 中搜索的时候支持基本正则不支持扩展正则

shell脚本

原创文章,作者:sqjl,如若转载,请注明出处:http://www.178linux.com/83933

(0)
上一篇 2017-08-05 19:25
下一篇 2017-08-05 19:57

相关推荐

  • linux网络配置

    主要内容: ip地址以及子网划分 路由基本概念 网络配置工具:ifconfig,ip,netstat使用 网卡配置文件及修改 IP地址: 它们可唯一标识 IP 网络中的每台设备 v  每台主机(计算机、网络设备、外围设备)必须具有唯 一的地址 v IP地址由两部分组成:   &n…

    Linux干货 2016-09-07
  • 文件通配符与命令行扩展

    * 匹配零个或多个字符 ? 匹配任何单个字符 ~ 当前用户家目录 ~mage 用户mage家目录 ~+ 当前工作目录 ~- 前一个工作目录 [0-9] 匹配数字范围 [a-z] 字母 [A-Z]字母          [a-Z] 会以aAbBcC…小大小大列出,特别要注意 [wang] 匹配列表中的任何的一个字符 [^wang]匹配列表中的所有字…

    2017-11-12
  • 文本处理、正则表达式、cut、grep、egrep、fgrep

    文本处理、正则表达式、cut、grep、egrep、fgrep Linux中文本处理工具最常用的就是文本处理三剑客grep、sed、awk再配合正则表达式,可以实现足够多的文本处理功能。工具的强大之处是因为使用它的人,如何才能发挥文本处理工具的作用呢?答案就是正则表达式,其实正则表达式,只是一种思想,一种表示方法,只要我们使用的工具支持表示这种思想那么这个工…

    Linux干货 2016-08-07
  • N22-第十一周作业

    1、详细描述一次加密通讯的过程,结合图示最佳。 (1)数字签名 A与B通信,B发给A一段数据,为了证明数据确实是B发送过来的,B首先会用单向加密算法从数据中提取一段特征码,然后用自己的私钥加密这段特征码和原始数据后,发送给A;A接受到数据,首先用B的公钥解密,获取到特征码和原始数据;然后用同样的单向加密算法从原始数据中提取一段特征码,与之前用公钥解密得到的特…

    Linux干货 2016-11-01
  • grep与文本处理工具

    grep:基本正则表达式,-E  -F egrep:扩展正则表达式,-G -F fgrep: 不支持正则表达式,-F  egrep:          支持扩展的正则表达式实现类似于grep文本过滤功能; grep -E          …

    Linux干货 2016-12-23
  • N22-第十一周作业

    第十一周作业 1、详细描述一次加密通讯的过程,结合图示最佳 (1)为了做到数据的安全,应该同时满足 保密性 完整性 可用性 (2)假设A,B通信,A是客户机,B是服务器 a、客户端向服务器端发送自己支持的加密方式,并且向服务器端请求其CA颁发给的证书 b、服务器选择共同支持的加密方式并发送自己的证书; c、客户端收到其证书,并验证证书,证书必须同时满足以下条…

    Linux干货 2016-12-06