Shell 脚本作业(8月11号)

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
echo "The hosname is: $(hostname)"
echo "The kernel version is: $(uname -r)"
echo "The server_ip is: $( ifconfig |grep 'inet\b' |grep -v '127.0.0.1' |sed 's/.*addr:\b//' |sed 's/Bcast.*//')"
echo "The CPU is: $(lscpu |grep -i 'model name' |tr -s ' ' |sed 's/.*://')"
echo "The OS version is: $(cat /etc/redhat-release)"
echo "The memorysize is: $(free |sed -n '2p' |tr : ' ' |tr -s ' ' |cut -d' ' -f2)"
echo "The disksize is: $(fdisk -l |sed -n '2p')"
[root@localhost bin]# bash -n systeminfometion.sh 
[root@localhost bin]# bash systeminfometion.sh 
The hosname is: localhost.localdomain
The kernel version is: 2.6.32-642.el6.x86_64
The server_ip is: 10.1.253.35  
The CPU is:  Intel(R) Core(TM) i3-2350M CPU @ 2.30GHz
The OS version is: CentOS release 6.8 (Final)
The memorysize is: 1004108
The disksize is: Disk /dev/sda: 128.8 GB, 128849018880 bytes
[root@localhost bin]# bash -n systeminfometion.sh 
[root@localhost bin]# vim systeminfometion.sh
You have new mail in /var/spool/mail/root

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
backdir="/root/etc$(date +%F)"
echo $backdir
cp -a /etc/. $backdir && echo "backup finished"
unset backdir
[root@localhost ~]# ll
drwxr-xr-x. 75 root root 4096 Aug 12 05:11 etc2016-08-11

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  显示当前硬盘分区中空间利用率最大的值
disk_max=$(df |tr -s ' ' % |cut -d% -f5 |grep '[0-9]' |sort -nr |head -1)
echo "The diskused_max is $disk_max"
unset disk_max
[root@localhost bin]# bash disk.sh 
The diskused_max is 53

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数   从大到小排序
netstat -nt |tr -s ' ' |cut -d' ' -f5 |grep '[0-9]' |sed 's/:.*//' |sort |uniq -c |sort
[root@localhost bin]# bash link.sh
 1 10.10.10.1
 1 10.1.250.69
 3 10.1.50.10

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  计算/etc/passwd文件中的第10个用户和第20用户的ID之和
user10_id=$(sed -n '10p' /etc/passwd |cut -d: -f3)
user20_id=$(sed -n '20p' /etc/passwd |cut -d: -f3)
sumid=$[$user10_id+$user20_id]
echo "The sum_id is: $sumid"
unset user10_id
unset user20_id
unset sumid

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
file1=$(grep '^$' $1 |wc -l)
file2=$(grep '^$' $2 |wc -l)
sumspace=$[$file1+$file2]
echo "The total spacelines is $sumspace"
unset file1
unset file2
[root@localhost bin]# bash sumspace.sh  /etc/fstab /etc/profile
The total spacelines is 12

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   统计/etc, /var, /usr目录中共有多少个一级子目录和文件
etc_sum=$(ls -A /etc |wc -l)
var_sum=$(ls -A /var |wc -l)
usr_sum=$(ls -A /usr |wc -l)
sumfile=$[$etc_sum+$var_sum+$usr_sum]
echo "The total file is $sumfile"
unset etc_sum
unset var_sum
unset usr_sum
[root@localhost bin]# bash sumfile.sh 
The total file is 208

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
[ $# -lt 1 ] && echo "please give a argument" || grep '^$' $1 |wc -l
[root@localhost bin]# bash argsnum.sh /etc/fstab
1

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  接受一个主机的IPv4地址做为参数,测试是否可连通。如果能   ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
[ $# -ge 1 ] && echo "$1" |egrep -o '(\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))' &> /dev/null && (ping -c 3 $1 &>/dev/null && echo -e "\e[31m this ip is accessed\e[0m" || echo -e "\e[31m this ip is not accessed \e[0m")
[root@localhost bin]# bash hostping.sh 10.1.1.1
this ip is not accessed 
[root@localhost bin]# bash hostping.sh 10.1.253.35
this ip is accessed

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

[root@localhost tmp]# chmod -rw file1
[root@localhost tmp]# ll file1
----------. 1 user1 user1 0 Aug 12 08:10 file1
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:    判断当前用户对/tmp/fiile1文件是否不可读且不可写
[ -r /tmp/file1 -a -w /tmp/file1 ] && echo "Yous can not to read and writ file1"
[root@localhost bin]# bash per.sh 
Yous can not to read and writ file1

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   实现禁止和充许普通用户登录系统
[ -f /etc/nologin ] && (rm -f /etc/nologin;echo user enable login) || echo user disable login already
[root@localhost bin]# bash login.sh 
user disable login already

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 接受一个主机的IPv4地址做为参数,先判断是否合格IP,否,提示IP格式不合法并退出,是,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;   如果不可ping通,则提示用户“该IP地址不可访问
#!/bin/bash
[ $# -ge 1 ] && echo "$1" |egrep -o '(\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))' &> /dev/null && (ping -c 3 $1 &>/dev/null && echo -e "\e[31m this ip is accessed\e[0m" || echo -e "\e[31m this ip is not accessed \e[0m")
[root@localhost bin]# bash hostping.sh 10.1.1.1
 this ip is not accessed 
[root@localhost bin]# bash hostping.sh 10.1.253.35
 this ip is accessed

12、计算1+2+3+…+100的值

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   计算1+2+3+...+100的值
echo {1..10} |tr ' ' + |bc
[root@localhost bin]# bash sum1.sh 
5050

i=1
sum=0
for i in {1..100};do
    sum=$[$sum+$i]
    i=$i+1
done
echo "the sum is $sum"
unset i
unset sum
[root@localhost bin]# bash sum.sh 
the sum is 5050

13、计算从脚本第一参数A开始,到第二个参数B的所有数字的总和,判断B是否大于A,否提示错误并退出,是则计算之

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  计算从脚本第一参数A开始,到第二个参数B的所有数字的总和,判断B是否大于A,否提示错误并退出,是则计算之
#!/bin/bash
[[ $1 -lt $2 ]] && (echo $(seq $1 $2) |tr ' ' + |bc) || echo "error,please input effective number"
[root@localhost bin]# bash f1.sh 10 30
420
[root@localhost bin]# bash f1.sh 100 30
error,please input effective number

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

(0)
pingskypingsky
上一篇 2016-08-15
下一篇 2016-08-15

相关推荐

  • Linux基础—磁盘管理

    1、创建一个10G分区、并格式化为ext4文件系统 1)要求其block大小为2048,预留空间百分比2,卷标MYDATA,默认挂载属性包含acl 2)挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳 #mkfs.ext4 -b 2048 -m 2 -L my…

    Linux干货 2016-11-17
  • vsphere虚拟机下的Linux在线移除硬盘后的报错处理(基于LVM)

    1)故障描述( vmware虚拟机,在线移除一块被设置为pv格式的硬盘后,提示unknown device错误): 说明:/dev/sdb1 /dev/sdc1 /dev/sdd1是组成/dev/vg_test/lvm_test的PV,这里我们模拟/dev/sdd1硬盘被直接断电了 [root@docker mnt]# pvdisplay…

    Linux干货 2016-07-29
  • N25_第二周作业_leon

    第二周博客作业_leon 1.       Linux上的文件管理类命令有哪些?其常用的使用方法及其相关示例演示 常用文件管理类命令有: mkdir、touch、rmdir、cp、rm、mv   mkdir—-创建目录 -m  直接设置权限(非默认权限) -v&nbs…

    Linux干货 2016-12-23
  • select和case用法

    一、作业 1、斐波那契数列又称黄金分割数列,因数学家列昂纳多·斐波那契以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2) 写一个函数,求n阶斐波那契数列 2、汉诺塔(又称河内塔)问…

    Linux干货 2016-08-21
  • centos6启动故障及修复

    前言 前文中我们熟悉了linux的启动流程,如果在启动过程中文件丢失或者损坏了怎么办呢?难道只有重装系统这一条路吗?那我们不是成了普通网管了吗?作为专业的运维工程师,我们需要掌握如何快速修复系统启动。 实验1: 破坏/boot/grup/下的文件——-stage2 阶段  不影响启动  mv /boot/grub …

    2017-09-03
  • Linux运维学习历程-第九天-bash脚本初步了解

    概述:   本章重点在于讲解bash脚本的基础知识,为今后学习使用bash脚本打下基础 一、bash基础特性         程序:指令+数据             指令:由程序文件提供         &n…

    Linux干货 2016-08-18