集中练习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
下一篇 2017-12-05

相关推荐

  • 系统基础之用户,组管理作业题

    、创建testuser uid 1234,主组:bin,辅助组:root,ftp,shell:/bin/csh home:/testdir/testuser 1 2 3 [root@wen-7 ~]# useradd -u 1234 -g bin -G root,ftp&nbsp…

    Linux干货 2016-08-04
  • 第三次 练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 ~]# who | cut -d "" -f1 |uniq root     pts/0        2016-11-13 12:17 (172.16.220.14) 2、取出最后登录到…

    Linux干货 2016-11-16
  • Linux basics–part1

    一、计算机的组成及其功能 依据冯·诺依曼体系结构,计算机可分为五大部分,CPU的运算器和控制器、内存、输入、输出。 CPU运算器:计算机中执行各种算术和逻辑运算操作的部件。运算器的基本操作包括加、减、乘、除四则运算,与、或、非、异或等逻辑操作,以及移位、比较和传送等操作,亦称算术逻辑部件(ALU)。运算器由算术逻辑单元(ALU)、累加器、状态寄存器、通用寄存…

    Linux干货 2017-07-10
  • 第二周练习与作业

    第二周作业 1、Linux上的文件管理类命令有哪些,其常用的使用方法及其相关示例演示          文件管理类命令:cp,mv,rm cp: 源文件;目标文件          [root@loc…

    Linux干货 2017-08-09
  • Because LVM so cattle(Logical Volume Manager)

    LVM: Logical Volume Manager         LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。 …

    Linux干货 2016-08-29
  • rsync3.0 (初稿 逐步完善)

    实验环境为 centos 7.2 系统   CentOS Linux release 7.2.1511 (Core)   主机  ip               10.1.250.19 客…

    Linux干货 2016-06-03

评论列表(1条)

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

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