集中练习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)
N27_sapbcsN27_sapbcs
上一篇 2017-12-04 21:20
下一篇 2017-12-05 10:53

相关推荐

  • systemd和systemctl

    systemd:      POTS–>boot Sequence(BIOS)–>Bootloader(MBR)–>kernel(ramdisk)–>rootfs(swithroot)–>/sbin/init    &n…

    Linux干货 2016-09-22
  • Linux的任务计划

    Linux的任务计划         我们在使用系统的过程中,经常需要在以后的某个时间点执行一个任务,而我们又不在机器旁边的时候,比如在半夜时服务器访问的用户很少,宽带资源很充足的时候,要去镜像软件包、重启服务器(虽然Linux很稳定)等,或者需要服务器在每天的特定时间去备份数据…

    2017-03-19
  • 程序包管理与编译安装详解

    (注:内容太多,容纳不下,编译安装apache单独成文) 一、程序包管理工具 不同的发行版采用的管理工具不尽相同:               debian: 程序文件以deb结尾,包管理器为dpkg    &…

    Linux干货 2016-08-24
  • Linux救援模式

        学习了Linux的启动流程,为了加深理解,今天做了如下的实验:删除掉Linux系统在启动过程中比较重要的几个文件,查看启动时出现的现象。加深对知识的理解。学习机器因为某些文件的缺失的修复方式。     一、删除grub.conf    &nb…

    Linux干货 2016-11-23
  • Linux的启动流程

    Linux的启动流程大致上如下图. 现在详细说明一下每个步骤: 第一阶段 当系统启动时,系统首先会加载BIOS。BIOS的首先会检查各硬件设备,当检查完毕没有问题之后。BIOS会根据设定的BootSequence来寻找可以引导系统的设备。一般而言,Linux是通过磁盘上MBR来引导系统的。 第二阶段 MBR是Master Boot Record,是位于磁盘第…

    Linux干货 2016-02-28
  • 在 Linux 下你所不知道的 df 命令的那些功能

    原文出处: xmodulo   译文出处:linux.cn – mtunique   欢迎分享原创到运筹维幄 问题: 我知道在Linux上我可以用df命令来查看磁盘使用空间。你能告诉我df命令的实际例子使我可以最大限度得利用它吗?   对于磁盘存储方面,有很多命令行或…

    Linux干货 2015-03-02

评论列表(1条)

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

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