集中练习6-bash脚本

集中练习6-bash脚本

1. 写一个脚本,使用ping命令探测172.16.250.1-172.15.250.254之间所有主机的在线状态;在线的主机使用绿色显示;不在线的主机使用红色显示
“`
#!/bin/bash
#
for i in {1..254}; do
ping 172.16.250.$i -w 1 -c 1 &> /dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo -e “\033[32mHost 172.16.250.$i is online.\033[0m ”
else
echo -e “\033[31mHost 172.16.250.$i is offline.\033[0m ”
fi
done
“`
2. 如何给网络配置多个地址,有哪些方式?
“`
(1) 通过ip命令添加
~]# ip addr add <IP_ADDR>/<MASKLEN> dev <IFACE_NAME>
(2) 针对CentOS7,使用nmtui命令添加
(3) 使用ifconfig命令
~]# ifconfig IFACE_LABEL IPADDR/NETMASK
(4) 可以通过复制原有配置文件到新文件IFACE:0,然后修改IP地址,网卡别名不支持动态获取地址
“`
3. 写一个脚本,完成以下功能
(1) 假设某目录(/etc/rc.d/rc3.d)下分别有K开头的文件和S开头的文件若干;
(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;
(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;
(4) 分别统计S开头和K开头的文件各有多少?
“`
#!/bin/bash
#
declare -i numk=0
declare -i nums=0
for files in `ls /etc/rc.d/rc3.d`; do
if [[ $files =~ ^K[[:digit:]].* ]]; then
echo “$files stop.”
let numk+=1
elif [[ $files =~ ^S[[:digit:]].* ]]; then
echo “$files start.”
let nums+=1
fi
done
echo “The number for Kfiles is $numk.”
echo “The number for Sfiles is $nums.”
“`
4. 写一个脚本,完成以下功能
(1) 脚本能接受用户名作为参数;
(2) 计算此些用户的ID之和;
“`
#!/bin/bash
#
echo -e “This is a script for calculate the summation for the user UID you entered, you can enter as many times as you want until you enter quit, and the script will calculate the summation for all these users’ UID for you. \n”
read -p “Please enter a username: ” user
declare -i sum=0
until [ $user == ‘quit’ ]; do
if id $user &> /dev/null; then
declare -i UserID=`id -u $user`
let sum+=$UserID
read -p “Please reenter a username: ” user
else
echo “User $user does not exist.”
read -p “Please reenter a username: ” user
fi
done
echo “The summation of UID for all the users you entered is $sum.”
“`
5. 写一个脚本
(1) 传递一些目录给此脚本;
(2) 逐个显示每个目录的所有一级文件或子目录的内容类型;
(3) 统计一共有多少个目录,且一共显示了多少个文件的内容类型;
“`
#!/bin/bash
#
[ -e /tmp/filetype.txt ] && rm -f /tmp/filetype.txt
read -p “Please enter a directory: ” dirc
declare -i typenum=0
declare -i dircnum=0
until ls $dirc &> /dev/null; do
echo “The directory you entered does not exist.”
read -p “Please enter a directory: ” dirc
done
touch /tmp/filetype.txt
until [ $dirc == ‘quit’ ]; do
let dircnum+=1
for dir_file in `ls $dirc`; do
file_type=`file -b $dirc/$dir_file | cut -d’,’ -f1`
echo $file_type | grep “symbolic link” &> /dev/null && file_type=’symbolic link’
cat /tmp/filetype.txt | grep “$file_type” &> /dev/null
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo $file_type >> /tmp/filetype.txt
let typenum+=1
fi
done
read -p “Please enter a directory: ” dirc
done
echo “You totally entered $dircnum directories.”
echo “There are $typenum types of file in the directories you entered.”
rm -f /tmp/filetype.txt
“`
6. 写一个脚本
(1) 通过命令行传递一个参数给脚本,参数为用户名;
(2) 如果用户的id号大于等于500,则显示此用户为普通用户;
“`
#!/bin/bash
#
read -p “Please enter a user: ” user
until [ $user == ‘quit’ ]; do
if ! id $user &> /dev/null; then
echo “The user you entered doesn’t exist.”
read -p “Please enter a user: ” user
else
declare -i UserID=`id -u $user`
if [ $UserID -ge 500 ]; then
echo “User $user is a common user.”
fi
read -p “Please enter a user: ” user
fi
done
“`
7. 写一个脚本,用ping命令测试172.16.250.20-172.16.250.100之间有哪些主机在线,将在线的显示出来;
“`
#!/bin/bash
#
for i in {20..100};do
ping -c 1 -W 1 172.16.250.$i &> /dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo “Host 172.16.250.$i is online.”
fi
done
“`
8. 打印九九乘法表;

for循环
“`
#!/bin/bash
#
for a in {1..9}; do
for b in `seq 1 $a`; do
echo -n -e “${b}X${a}=$[${b}*${a}]\t”
done
echo
done
“`
while循环
“`
#!/bin/bash
#
declare -i a=1

while [ $a -le 9 ]; do
declare -i b=1
while [ $b -le $a ]; do
echo -n -e “${b}X${a}=$[${b}*${a}]\t”
let b++
done
let a++
echo
done
“`
until循环
“`
#!/bin/bash
declare -i a=1

until [ $a -gt 9 ]; do
declare -i b=1
until [ $b -gt $a ]; do
echo -n -e “${b}X${a}=$[${b}*${a}]\t”
let b++
done
let a++
echo
done
“`
9. 打印逆序的九九乘法表;

for循环
“`
#!/bin/bash
#
for a in `seq 1 9 | sort -r`; do
for b in `seq 1 $a | sort -r`; do
echo -n -e “${b}X${a}=$[${b}*${a}]\t”
done
echo
done
“`
while循环
“`
#!/bin/bash
#
declare -i a=9
while [ $a -ge 1 ]; do
declare -i b=$a
while [ $b -ge 1 ]; do
echo -n -e “${b}X${a}=$[${b}*${a}]\t”
let b–
done
let a–
echo
done
“`
until循环
“`
#!/bin/bash
#
declare -i a=9
until [ $a -lt 1 ]; do
declare -i b=$a
until [ $b -lt 1 ]; do
echo -n -e “${b}X${a}=$[${b}*${a}]\t”
let b–
done
let a–
echo
done
“`

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

(0)
上一篇 2017-12-04 21:20
下一篇 2017-12-05 10:53

相关推荐

  • 使用NFS和Samba文件服务搭建博客站点

    使用NFS和Samba文件服务搭建博客站点 实验目的:分别使用NFS和Smaba文件服务实现wordpress 实验要求: (1) server导出/data/application/web,在目录中提供wordpress; (2) client挂载nfs server导出的文件系统至/var/www/html;(3) 客户端1(lamp),部署w…

    2017-04-30
  • 第四周作业

    1. 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限 [root@localhost ~]# chmod -R g=,o=  /home/tuser1 [root@localhost home]# ll drwx——  3 root&…

    Linux干货 2016-12-26
  • linux下文件类型简介

    linux下文件类型简介 概述 文件类型缩写及别称 文件类型的缩写、翻译及别称 文件类型 缩写 英文 其他名称 普通文件 – Regular file 目录文件 d Directory file 块设备文件 b Block special file 块区文件 字符设备文件 c Character special file 字符设备文件 数据传送 …

    Linux干货 2016-10-17
  • 计算机;GPL等开源协议;Linux 哲学思想的炼成;MBR & GPT—fazion in magedu 20.July

    入门Linux的基础知识 fazionlan blog in 178Linux.July 22nd. 第一台计算机 1946第一台计算机ENIAC(electronic numerical integrator and calculator)在美国滨州大学诞生,是美国奥伯丁武器试验场为了满足计算弹道而研制的 冯诺依曼体系结构:1946年数学家提出运算器、控制…

    Linux干货 2016-07-29
  • LVS介绍及工作原理图解

    一、LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器,是一个由章文嵩博士发起的自由软件项目,官方站点是:http://www.linuxvirtualserver.org。现在LVS已经是Linux标准内核的一部分,在Linux2.4内核以前,使用LVS时必须重新编译内核以支持LVS功能模块,但是从Linux2….

    2017-12-06
  • 第五周作业

    1、显示当前系统上root、fedora或user1用户的默认shell; 答:#egrp ‘^(root|fedora|user1)’ /etc/passwd | cut -f1,7 -d: 2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello(); 答:#egrp -o &…

    Linux干货 2017-02-25

评论列表(1条)

  • 马哥教育
    马哥教育 2017-12-08 16:40

    脚本没有问题,建议排版可以截图下来。不然不好分析,也不好看。