脚本基础课后练习

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

#!/bin/bash

echo “MY hostname is `hostname`”
echo “My IPv4 address is `ifconfig | egrep -w “inet” | head -n1 | tr -s ” ” “:” | cut -d: -f4` ”
echo “My Kernel is `cat /etc/redhat-release` ”
echo “My CPU is `lscpu | egrep “Model name” | tr -s ‘ ‘ | cut -d: -f2`”
echo “My Mem is `free | egrep “Mem” | tr -s ” ” “:” | cut -d: -f2`”
echo “My Disk is `lsblk | egrep “^sda” | tr -s ” ” | cut -d” ” -f4`”

(2)编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
答:
#!/bin/bash
#
echo “copy start…”
sleep 5
cp -a /etc/ /data/etc`date +%F`
echo “copy finished”

(3)编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
答:
#!/bin/bash
#
df | egrep “^/dev/sd” | tr -s ” ” “%” | cut -d% -f5 | sort -nr | head -n1

(4)编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
答:
#!/bin/bash
#
sort access_log | cut -d” ” -f1 | uniq -c | sort -nr
或者:
sort access_log | egrep -o “^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\>” | uniq -c | sort -nr

(5)编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第 20用户的ID之和
答:
#!/bin/bash
#
uid10=”`cat /etc/passwd | head | tail -n1 | cut -d: -f3`”
uid20=”`cat /etc/passwd | head -n20 | tail -n1 | cut -d: -f3`”
usum=”$[ uid10 + uid20 ]”
echo $usum

(6)编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
答:
#!/bin/bash
#
space1=”`cat $1 | egrep “^[[:space:]]*$” | wc -l`”
space2=”`cat $2 | egrep “^[[:space:]]*$” | wc -l`”
spacesum=”$[ space1 + space2 ]”
echo $spacesum

(7)编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级 子目录和文件
答:
#!/bin/bash
#
file1=”`ls $1 | wc -l`”
file2=”`ls $2 | wc -l`”
file3=”`ls $3 | wc -l`”
sumfile=”$[ file1 + file2 + file3 ]”
echo $sumfile

(8)编写脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数 不小于1,则显示第一个参数所指向的文件中的空白行数
答:
#!/bin/bash
#
[ “$#” -lt 1 ] \
&& { echo “please input a path”; exit; } \
|| { space=”`cat $1 | grep -c “^[[:space:]]*$”`”; echo “The file space lines are $space”; }

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

ping -c1 $1 &>/dev/null && echo “host up” || echo “time out”

(10)编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
答:
#!/bin/bash
#
disk=”`df | egrep “^/dev/sda” | tr -s ” ” % | cut -d% -f5 | sort -nr | head -n1`”
inode=”`df -i | egrep “^/dev/sda” | tr -s ” ” % | cut -d% -f5 | sort -nr | head -n1`”
[ “$disk” -ge 80 ] && echo “warning!!!The disk will be full”
[ “$inode” -ge 4 ] && echo “warning!!!The inode will be full”

(11)编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写
答:
#!/bin/bash
#
[ “$#” -lt 1 ] && { echo “please input a argument”;exit; }
[[ -r $1 ]] && [[ -w $1 ]] && echo “yes” || echo “no”

(12)编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
答:
#!/bin/bash
#
[[ -f $1 ]] && [[ “$1” == *.sh ]] && chmod a+x “$1” || echo “no .sh file”

(13)编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
答:
①For nologin:
#!/bin/bash
#
[[ -e /etc/nologin ]] && echo “no login” || { touch /etc/nologin ; echo “no login”; }
②For login:
#!/bin/bash
#
[[ -e /etc/nologin ]] && { rm -f /etc/nologin ; echo “access login”; } || echo “access login”

(14)让所有用户的PATH环境变量的值多出一个路径,例如: /usr/local/apache/bin
答:①vim /etc/profile ②找到export PATH USER在下面一行输入export PATH=$PATH:/usr/local/apache/bin ③. /etc/profile 使其生效。

(15)用户root登录时,将命令指示符变成红色,并自动启用如下别名: rm=‘rm –i’ cdnet=‘cd /etc/sysconfig/network-scripts/’ editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’ editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ’ (如果系 统是CentOS7)
答:①vim ~/.bashrc ②alias cdnet=’cd /etc/sysconfig/network-scripts/’
alias editnet=’vim /etc/sysconfig/network-scripts/ifcfg-eth0′
alias editnet=’vim /etc/sysconfig/network-scripts/ifcfg-eno16777736′
alias rm=’rm -i’

(16)任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
答:在[root@Centos6 profile.d]#下建立脚本vim dangerous.sh
#!/bin/bash
#
echo -e “\033[31m ###################################################\033[0m”
echo -e ” \033[31m hi,dangerous!!!\033[0m”
echo -e “\033[31m ###################################################\033[0m”

 

 

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

(0)
烟花儿烟花儿
上一篇 2018-04-13 17:32
下一篇 2018-04-13 20:43

相关推荐

  • shell脚本逻辑运算及条件判断

    shell脚本基础,判断与运算命令用法大全

    2018-04-10
  • 新建虚拟机+安装Centos7

    一、基本环境 笔记本电脑:widows 7 虚拟机版本:vmware workstation 12 镜像版本:CentOS-7-x86_64-Everything-1804 下载地址:mirrors.aliyun.com www.centos.org 一、新建虚拟机 1.创建虚拟机 2.选择“典型”,点击下一步。 3.选择“稍后安装系统”,点击下一步。 4.…

    2018-07-22
  • ansile

    ansible ansible 是一个自动化运维工具,他是基于python语言实现,基于openssh安全的工具 特性: 1)幂等性:一个任务执行1遍和执行n遍效果一样 ,不因重复执行带来意外情况2)无需代理不依赖PKI(无需ssl)3)可使用任何编程语言写模块4)YAML格式,编排任务,支持丰富的数据结构5)较强大的多层解决方案 ansible可以直接由一…

    Linux笔记 2018-06-04
  • 随堂笔记2

    记马哥Linux运维课程第二周知识点(持续更新中)   bin目录 存放二进制数据 给普通用户执行的 在Centos7上 绿色的为… 并不是真正的文件夹 而是快捷方式 bin->usr/bin 内容放在一起 ;在6上,这两者并不是同一个目录,分开放 sbin 给管理员执行的二进制程序 media mnt(mount) 充当外围设备…

    Linux笔记 2018-07-29
  • 关于shell脚本编程的基础知识理解介绍

    shell:     Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。 shell脚本:是一种为shell编写的脚本程序,其编辑风格可以分为过程式和对象式。 过程式:是以指令为中心且数据服务于指令。 对象式:是以数据为中心且指令服务于数据。 shell基本编程概念和环境: She…

    2018-04-16
  • 运维自动化之系统安装部署

    自动化安装centos系统

    2018-05-24