脚本基础课后练习

(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

相关推荐

  • linux网络基础

    无线网络标准: 中国:早期wapi后期wapi+wifi 国际:wifi 无线模式802.11 a/b/n/ac/bn,以太模式IEEE  802.3 网络施工模式: ethtool eth0 单工:单向传输 双工:双向传输 全双工:同时双向 半双工:轮流双向 集线器Hub :由于网线信号电流强度受距离影响,所以集线器很好的提供了电能 冲突域:同个网域中发送…

    Linux笔记 2018-05-07
  • shell笔记

    在学习的时候整理的零散笔记

    Linux笔记 2018-04-14
  • 第二周总结

    反向单引号 ` `:执行能力强,可以执行命令 [等价于$()]单引号 ‘ ’:只显示字符双引号 “ ”:识别变量,不识别命令花括号{ }: 里面内容互相组合 打印重复字符串的简化形式echo file{1,3,5} 结果为:file1 file3 file5rm -f file{1,3,5} 删除file1 file3 file5echo file{1..1…

    Linux笔记 2018-07-29
  • 文本处理工具

    文本处理小工具 tr tr [选项]…SET1 [SET2] 从标准输入中替换、缩减和/或删除字符,并将结果写到标准输出。 ​ -c:取字符集的补集 ​ -d:删除所有属于第一字符集的字符 ​ -s:把连续重复的字符以单独一个字符表示(压缩) ​ -t:将第一个字符集对应字符转化为第二字符集对应的字符 #echo ‘tank zhang’ |tr a-z A…

    Linux笔记 2018-05-10
  • shell脚本之判断httpd是否有异常

    案例 以web为例 大全讲解:如http为例 #/etc/init.d/httpd start      开启httpd #lsof –i :80 [root@centos6 ~/bin]$curl -I -s -o /dev/null -w “%{http_code}\n” http://172.16.0.1 析:-I 是响应头,响…

    Linux笔记 2018-05-20
  • shell脚本

    shell脚本的练习题

    2018-04-18