shell 脚本之基础篇作业

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‘)"
[[email protected] bin]# bash -n systeminfometion.sh 
[[email protected] 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
[[email protected] bin]# bash -n systeminfometion.sh 
[[email protected] 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
[[email protected] ~]# 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
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] 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")
[[email protected] bin]# bash hostping.sh 10.1.1.1
this ip is not accessed 
[[email protected] bin]# bash hostping.sh 10.1.253.35
this ip is accessed

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

[[email protected] tmp]# chmod -rw file1
[[email protected] 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"
[[email protected] 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
[[email protected] 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")
[[email protected] bin]# bash hostping.sh 10.1.1.1
 this ip is not accessed 
[[email protected] 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
[[email protected] 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
[[email protected] 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"
[[email protected] bin]# bash f1.sh 10 30
420
[[email protected] bin]# bash f1.sh 100 30
error,please input effective number
时间: 2024-07-30 01:14:01

shell 脚本之基础篇作业的相关文章

Linux中的shell脚本编程——基础篇

概述: shell脚本在Linux系统管理员的运维工作中非常重要.shell脚本能够帮助我们很方便的管理服务器,因为我们可以指定一个任务计划,定时的去执行某一个脚本以满足我们的需求.本篇将从编程基础.脚本基本格式.变量.运算.条件测试这几个方面详细介绍shell脚本编程的基础内容,也是我们必须要掌握熟练的内容. 一.编程环境 1.程序:指令+数据 程序编程的风格有两种: 过程式:以指令为中心,数据服务与指令 对象式:以数据为中心,指令服务于数据 2.程序的执行方式: □计算机:只能识别二进制文件

shell 脚本编程基础篇

一级标题 二级标题 1.编程基础 Linus:Talk is cheap, show me the code 程序组成 程序:算法+数据结构 数据:是程序的核心 算法:处理数据的方式 数据结构:数据在计算机中的类型和组织方式 面向过程语言 做一件事情,排出个步骤,第一步干什么,第二步干什么,如果出现情况A,做什么处理,如 果出现了情况B,做什么处理 问题规模小,可以步骤化,按部就班处理 以指令为中心,数据服务于指令 C,shell 面向对象语言 一种认识世界.分析世界的方法论.将万事万物抽象为各

Shell脚本编程——基础篇

Shell脚本概念 1.将要执行的命令按顺序保存到一个文本文件2.给该文件可执行权限,便可运行3.可结合各种shell控制语句以完成更复杂的操作 Shell脚本应用场景 1.重复性操作2.批量事务处理3.自动化运维4.服务运行状态监控5.定时任务执行 完善的shell脚本构成 1.脚本声明2.可执行语句3.注释文字 Shell的作用--命令解释器,"翻译官" 执行脚本文件方法大全 方法一:脚本文件路径(绝对路径和相对路径)方法二:sh脚本文件路径方法三:source脚本文件路径拓展方法

shell脚本练习基础篇2

练习   2.写一个脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息   #!/bin/bash  #   case $1 in    yY]|[yY][eE][Ss])           echo "you  put a $1"            ;;              [nN]|[nN][Oo])   echo "you put a $1" ;;     *)       ech

Linux shell脚本编程基础之练习篇

shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash if [ $# -ne 1 ] then echo "请输入一个参数" exit else echo "参数正确" newfile=$1 fi #echo `grep "^#\!" ${newfile}` if ! grep "^#\!" ${newfile} &>/

shell脚本编程高级篇

SHELL脚本编程进阶循环执行:简单来说就是把一些指令重复循环. 循环代码具体的指令有三种: for , while , until其中for, while用的最多.for循环 for 变量名 in 列表;do 循环体 done 关键字的帮助都是用help来查询.for循环语法:在shell编程中 for,in,do,done.这些都是他的关键字,其中循环的指零就放在do和done之间.WORDS决定了循环次数.循环的次数由in 后面跟的WORDS(字符串)的数量决定.字符串的个数决定了do和d

Shell脚本初级练习篇

Shell脚本初级练习篇 脚本1 作用:创建10个1M的文件 [[email protected] script]# cat make_file.sh  #!/bin/bash # for i in $(seq 1 10);do     dd if=/dev/zero of=/data/test/test"${i}" bs=1M count=1 done 脚本2 作用:移走/data/test目录下大于100K的普通文件到/tmp目录下 [[email protected] scrip

shell脚本的基础

shell脚本的基础 shell 基本语法 变量 什么是shell? 先看一个简单的shell程序 [[email protected]~]# cat linux.sh      #查看linux.sh文件内容#!/bin/bash echo -e "\e[1;31m linuxtouch  \e[0m"   #红色字体输出 linuxtouch[[email protected] ~]# sh linux.sh  linuxtouch  [[email protected] ~]#

shell脚本的基础1

shell脚本的基础 1.shell 脚本的编写规范 2 变量与特殊变量应用 3局部变量与全局变量 4 测试判断表达式 在一些复杂的Linux维护工作中,大量重复的输入和交互操作不但费时费力,而且容易出错. 编写脚本的好处: 批量的处理,自动化的完成维护,减轻管理员的负担. linux的shell脚本是一种特殊的应用程序,常见的shell解释器有很多种使用不同的shell时期内部指令命令提示方式方面会存在一些区别,可以通过/etc/shells文件查看 [[email protected] ~]