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)
sqjlsqjl
上一篇 2017-08-05 19:25
下一篇 2017-08-05 19:57

相关推荐

  • 在CentOS中获取命令帮助

    在CentOS中获取命令帮助    在使用和学习CentOS系统中,当我们遇上不熟悉的命令却又需要了解它的详细用法的时候,我们需要获取帮助。除了借助他人和搜索引擎之外,自己通过查看系统帮助文档来解决问题是很重要的。下面就来介绍如何获取帮助以及简单的解决思路:    一、如何获取命令帮助 Linux提供多层次的命令帮助:…

    Linux干货 2016-07-27
  • Linux的自动化安装实现

    在介绍自动化安装系统之前让我们来回顾一下系统的启动流程,系统的启动大概分为一下几个步骤:加电自检 boot sequence(mbr)— boot loader –>kernel(initramfs)—>rootfs  /sbin/init大概就是这么几个过程了,接下来来介绍系统的安装,安装系统虽然看起来简单,普…

    系统运维 2016-09-19
  • 实验:配置静态路由1

    实验:配置静态路由1 IP地址规划如下: 注意,环境准备: 1、MAC地址不要有冲突,如果是复制的虚拟机,对于centos6需删除网卡定义文件rm -f /etc/udev/rules.d/70-persistent-net.rules 2、清空防火墙iptables -F(查看:iptables -vnL) 3、启用IP转发功能:echo 1 > /…

    2017-03-26
  • selinux与systemd

    selinux :service enhanced linux 安全增强的Linux  软件的安全性 选择安全系数较高的系统提高现有系统的安全性  计算机的安全等级D  最低的安全级别,提供最少的安全防护,系统访问无限制。DOS   C  访问控制的权限,能够实现可控的安全防护,个人账户管理,审计和…

    Linux干货 2017-05-22
  • Linux系统中的Inode节点表

    索引节点是指在许多类Unix文件系统中的一种数据结构。每个索引节点保存了文件系统中的一个文件系统对象的元信息数据,但不包括数据内容或者文件名。

    2017-11-16
  • Linux三剑客之sed

     Sed简介       sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处 理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存…

    Linux干货 2016-08-12