shell脚本编写-1练习题

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

#!/bin/bash

echo “the hostname is:`hostname`”

echo “the ip address is:`ifconfig | sed –n ‘2p’ |sed 's/^.*inet//'|sed 's/net.*//'`”

echo “the op is: `cat /etc/centos-release`”

echo “the kernel version is: `uname -r`”

echo “the cpu type is: `lscpu |sed -n '13p'|sed 's/^.*://'|tr -s ' '`”

echo “the memory size is: `free –m |sed -n '2p'|sed 's/^.*://'|tr -s ' '|cut -d" " -f2`MB”

echo “the disk size is:`fdisk -l|sed -n '2p'|sed 's/^.*://'|sed 's/\,.*//'|tr –d ' '`”

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

echo “the lagerest disk partition is`df| grep 'sda'| tr -s ' ' ':'| cut -d: -f5 |sort |tail -1`”

4、编脚本/root/bin/links.sh,显示正连接本主机每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

#!/bin/bash

netstat -nt |tr -s ' '|cut -d' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr

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

#!/bin/bash

A10=`cat /etc/passwd|sed –n ‘10p’|cut –d: -f3`

A20=`cat /etc/passwd|sed –n ‘20p’|cut –d: -f3`

let sum=A10+A20

echo $sum

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

#!/bin/bash

A=`grep “^[[:space:]]*$” $1|wc -l`

B=`grep “^[[:space:]]*$” $2|wc -l`

let sum=A+B

echo $sum

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

#!/bin/bash

A=`ls –d /etc/* |wc -l`

B=`ls –d /var/* |wc -l`

C=`ls –d /usr/*|wc -l`

let sum=A+B+C

echo $sum

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

#!/bin/bash

[[ $# -lt 1 ]] && echo "less one arg"||echo "`grep '^[[:space:]]*$' $1 |wc -l`"

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

#!/bin/bash

ping -c1 -W1 $1 &> /dev/null && echo ping successfull || echo ping failture

10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,发邮件通知root磁盘满

#!/bin/bash

maxc=`(df;df -i)|grep "sd"|tr -s ' ' |cut -d' ' -f5|grep -o "[[:digit:]]\+"|sort -nr|head -1`

[ $maxc -ge 80 ] && mail -s “diskwarning” root < ~/fi || exit

11、指定文件做为参数,判断文件是否为.sh后缀,如果是,添加x权限

方法一:

#!/bin/bash

A=`echo $1 | grep -o "\.[^.]\+$"`

[[ "$A" == ".sh" ]] && chmod +x $1 || echo xxf

方法二:

#!/bin/bash

read -p "please input the file:" a

A1=`echo $a | grep -o "\.[^.]\+$"`

[[ "$A1" == ".sh" ]] && chmod +x $a || echo xxf

12、判断输入的IP是否为合法IP

#!/bin/bash

read -p "please input ip adress:" a

[[ $a =~

(([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\> ]] && echo legal || echo ilegal

13、计算1+2+3+…+100

#!/bin/bash

a=`echo {1..100} |tr ' ' '+'`

let sum100=a

echo $sum100

14、输入起始值A和最后值B,计算从A+(A+1)…+(B-1)+B的总和

#!/bin/bash

echo “the total count is:`seq + $1 $2 |bc`”

15、chmod -rw /tmp/file1,编写脚本/root/bin/per.sh,判断当前用户对/tmp/file1文件是否不可读且不可写

#!/bin/bash

[ ! -r /tmp/file1 -a ! -w /tmp/file1 ] && echo “`whoami`can’t rw” || echo “`whoami`can r or w”

[ ! \(-r /tmp/file1 -o -w /tmp/file1\) ] && echo “`whoami`can’t rw” || echo “`whoami`can r or w”

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

禁止普通用户登录:

#!/bin/bash

[ -e /etc/nologin ] && exit || touch /etc/nologin

echo "disable user login "

允许普通用户登录:

#!/bin/bash

[ -e /etc/nologin ] && rm -f /etc/nologin

echo "enable user login "

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

(0)
上一篇 2016-08-15 09:24
下一篇 2016-08-15 09:24

相关推荐

  • Linux 的发展史

    前言 l  免费源码开放 l  安全性高,漏洞修补快 l  多任务、多使用者 l  多平台支持 l  设备要求低,不耗资源 ……      看到这一系列的优点,在IT人的脑海里只会浮出一个名字Linux. 如果还不够直观的话,有这么一个数据, 显示前500系统中的485…

    Linux干货 2016-10-14
  • bash脚本之进阶

    bash脚本 1、终端打印 1、单双引号(echo) 双引号: ①在双引号里面打不出! ②双引号,可以在文本中使用“;”,“;”在bash中被用作命令定界符。 单引号: 变量替换在单引号中无效。 2、printf:不会自动添加换行符,要手动添加 如下: 结果: 3、补充: echo 要使用转义系列需要加e,!号除外 2、变量和环境变量 脚本语言通常…

    Linux干货 2016-11-28
  • 3 Ways to Check Bad Sectors For Hard Drive And SSD

    This Windows 10 recovery utility may stop working due to different reasons. Here, we’ll discuss the major reasons behind System Restore not working issue and the solutions to resol…

    Linux干货 2022-12-16
  • N25期–第十五周作业

    1、 总结sed和awk的详细用法; 2、删除/boot/grub/grub.conf文件中所有行的行首的空白字符; # sed ‘s@^[[:space:]]\+@@’ /boot/grub/grub.conf 3、删除/etc/fstab文件中所有以#开头,后跟至少一个空白字符的行的行首的#和空白字符; # sed ‘s@^#[[:space:]]*@@…

    Linux干货 2017-05-08
  • bash脚本编程语言if语句,find文件查找和压缩工具及课后作业练习

    一.概述 使用read命令来接受输入 使用read来把输入值分配给一个或多个shell变量: -p指定要显示的提示 -t TIMEOUT read从标准输入中读取值,给每个单词分配一个变量 所有剩余单词都被分配给最后一个变量 read -p “Enter a filename:“ FILE 过程式编程语言: 顺序执行 选择执行 循环执行 条件选择if语句 选…

    Linux干货 2016-08-22
  • find命令详解

    find命令是用来在给定的目录下查找符合给定条件的文件   find [OPTIONS] [查找起始路径] [查找条件] [处理动作]   一、OPTIONS参数     -P、-L、-H:控制软连接的对待方式,用的不多。不介绍了   二、查找路径     就是个目录路径,相对和绝对都可以。   三、查找条件     (一)、根据名称查找       -na…

    Linux干货 2016-09-19