集中练习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

相关推荐

  • N22-℡浮生.若夢 ╮-第二周作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 cp文件复制:       常用选项:     -i:交互式复制,即覆盖之前提醒用户确认;     -f:强制覆盖目标文件;  &n…

    Linux干货 2016-09-06
  • RAID级别介绍

    raid分为软raid和硬raid,一般公司使用硬raid,数据无价。 存数据都是先存到内存,后同步到硬盘,为提高raid卡的性能会在raid嵌入内存颗粒。 但是问题是断电后内存的数据没有同步到硬盘会丢失,这样就会有raid卡电池。(raid卡必须有raid电池) 计算机只识别raid卡,不会识别raid卡下有几块硬盘 raid 0:  (条带式)…

    Linux干货 2016-03-22
  • N22-第四周博客作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# ll -d  /etc/skel/drwxr-xr-x. 3 root root 74 3月&…

    Linux干货 2016-09-19
  • Linux基础目录名称命名法则及功能规定

    Linux中有非常多的目录文件,那么这些目录文件的命令规则,和功能都有那些,今天学习了下,下面是我的学习记录,跟大家分享下。 文件命名规则 (1) 除了/之外,所有字符都合法 (2) 特殊字符如@、#、¥、&、()、-、空格等最好不要使用,当使用空格作为文件名时,执行命令会出错 (3) 避免使用”.”作为文件名的第一个字符,因为在Linux系统中以”…

    Linux干货 2016-08-15
  • Linux的哲学思想

    Linux的哲学思想 一切皆文件 在linux中所有的一切,都是可以通过ls进行查询的到的,甚至可以说ll查看详细信息时表现形式为文件,所有的设备,所有的文件不论后缀,只是一个单纯文件通过vim可以编辑一切 单一目的的小程序,组合小程序完成复杂任务 在linux中,每一个命令都是对应一个功能,通过不同的参数来完成不同的要求,通过繁多的小的命令来完成大型的要求…

    Linux干货 2016-10-30
  • 第二周作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 目录管理类的命令: mkdir, rmdir mkdir:make directories mkdir [OPTION]… DIRECTORY… -p: 自动按需创建父目录; -v: verbose,显示详细过程; -m MODE:直接给定权限; 注意:路径…

    Linux干货 2018-01-17

评论列表(1条)

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

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