shell脚本

shell脚本的练习题

timg

1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: systeminfo.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo “The CPU of the system is:” `lscpu |grep “name” |tr -s ” ” |cut -d ” ” -f 5`
echo “The memory size of the system is:” `free -h |grep “Swap” |tr -s ” ” |cut -d” ” -f 2`
echo “The size of the system’s hard disk is :” `lsblk |grep -w “sda” |tr -s ” ” |cut -d ” ” -f 4`
echo “The system version is :” `cat /etc/centos-release |grep -o ” [0-9]”`
echo “The kernel version is :” `uname -r`
echo “The name of the system host :” `hostname`
echo “The IP of the system is :” `ifconfig ens33 |sed -n “2p” |sed “s/.*inet//” |sed “s/net.*//”`

 

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: backup.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo -e “\e[1;31mStart a backup…\e[0m”
sleep 5
cp -av /etc/ /data/etc`date “+%F”`
echo -e “\e[1;33mBackup completion\e[0m”

 

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: disk.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo “The maximum value of the hard disk is :” `df |grep “sda” |tr -s ” ” |cut -d” ” -f 5 |sort -nr |head -n1`

 

4.编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第
20用户的ID之和

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumid.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
UID1=`cat /etc/passwd |head -n10 | tail -n1 |cut -d: -f 3`
UID2=`cat /etc/passwd |head -n20 | tail -n1 |cut -d: -f 3`
let UID3=UID1+UID2
echo $UID3

 

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

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumspace.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
blabk1=`cat /$1 |grep -c “^$”`
blabk2=`cat /$2 |grep -c “^$”`
let blabk=blabk1+blabk2
echo $blabk

 

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

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumfile.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
file1=`ls -l /etc |grep -c -e “^d” -e “^-“`
file2=`ls -l /var |grep -c -e “^d” -e “^-“`
file3=`ls -l /usr |grep -c -e “^d” -e “^-“`
let file4=file1+file2+file3
echo $file4

 

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

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: hostping.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[[ $1 =~ “\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>” ]] && echo “Please enter a correct IP” && exit
ping -c1 $1>/dev/null && echo “The IP can be accessed” || echo “The IP is not accessible”

 

8.编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如
果超过80%,就发广播警告空间将满

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: checkdisk.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
disk=`df |grep “sda”|tr -s ” ” “%” |cut -d”%” -f 5 |sort -nr |head -n1`
[ “$disk” -ge 40 ] && echo “The use rate of disk partitions is now :” $disk && wall Space will be full

 

9. 编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如
果是,添加所有人可执行权限,否则提示用户非脚本文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: per.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ $# -ne 1 ] && echo “You have to enter one” && exit
[ ! -r $1 -a -w $1 ] && echo “The document is unreadable and cannot be written.” || echo “Read and write permissions to the file”

 

10. 编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统

创建nologin文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: nologin.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ -a /etc/nologin ] && echo “The nologin file exists! “|| (touch /etc/nologin; echo -e “\e[1;31mThe nologin file is created!\e[0m “;)

删除nologin文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: login.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ -a /etc/nologin ] && echo -e `rm -f /etc/nologin` -e “\e[1;31mThe file has been deleted!\e[0m” || echo “The file does not exist”

11. 让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin

echo $PATH |sed -r “s@(/usr/local/sbin:)@/usr/local/apache/bin:\1@”

 

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/96514

(1)
周亚飞周亚飞
上一篇 2018-04-18
下一篇 2018-04-18

相关推荐

  • LFS实验第一次——第一章

    环境准备:Centos6.8虚拟机 2G内存 2颗CPU 200G硬盘分区情况:/ 50G  /boot 1G  /swap 2G /data 30G 安装时吧开发工具安装上(gcc)

    2018-03-29
  • Linux的哲学思想

    1.一切皆文件
    2.小型,单一用途的程序
    3.配置信息保存在文本文件中
    4.尽量避免与用户交互

    Linux笔记 2018-05-11
  • 磁盘管理

    笔记

    2018-04-29
  • lvs(linux virtual server)、keepalived

    Linux Cluster: httpd: ab,  benchmark; 系统的扩展方式: Scale up:向上扩展; 提供性能更好的服务器替代现有的服务器; Scale out:向外扩展; 提供更多的服务器来满足同一个需求; 集群:将多台主机组织起来满足某一特定需求; 集群类型: 1、LB:Load Balancing, 负载均衡集群; 负载均衡器,调…

    2018-07-05
  • linux发行版的基础目录名称命名法则和功用规定

    一.文件名使用法则;      1.严格区分字符大小写      2.目录也是文件,在同一路径下,两个文件不能同名      3.文件名除了/以外的任意字符都可以使用,最长不超过255字符。      4.所有由.开头的文件都是隐藏文件。 二.文件系统:层级结构;有索引;      /:原初起点:      第二层结构;      第三层结构;      倒…

    Linux笔记 2018-05-13
  • Linux“文本三剑客”之grep

    Linux“文本三剑客”之grep grep,Global search REgular expression and Print out the line. 是Linux中常用的文本搜索工具,基于正则表达式(Regular Expression)和文本字符来编写模式(Pattern),然后根据指定的模式对目标文本逐行进行匹配,打印匹配到的行。 grep工具…

    2018-05-14