shell脚本编程练习

1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态

在线的主机使用绿色显示

不在线的主使用红色显示

#!/bin/bash

#

for i in {1..254};do

if ping -c 6 -w 1 192.168.1.$i &> /dev/null;then

echo -e “\033[32m 192.168.1.$i \033[0m is up”

else

echo -e “\033[31m 192.168.1.$i \033[0m is down”

fi

done

2、如何给网络接口配置多个地址,有哪些方式?

  1. a) 使用ifconfig命令

例如 ifconfig eno16777736:0 192.168.0.100/24

  1. b) 使用Ip addr命令

例如 ip addr add 192.168.0.101/24 dev eno16777736

  1. c) 通过配置文件/etc/sysconfig/network-scripts/ifcfg-IFACE来识别接口并完成配置;
  2. d) 使用nmtui命令

3、写一个脚本,完成以下功能

(1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干

(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串

(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串

(4) 分别统计S开头和K开头的文件各有多少

#!/bin/bash

#

declare -i n=0

declare -i m=0

 

for i in $(ls /etc/rc.d/rc3.d); do

 

if [ $(echo $i | cut -c 1)  == “K” ]; then

echo “$i stop”

let n++

elif [ $(echo $i | cut -c 1) == “S” ]; then

echo “$i start”

let m++

fi

 

done

 

echo “K is $n S is $m.”

4、写一个脚本,完成以下功能

(1) 脚本能接受用户名作为参数

(2) 计算此些用户的ID之和

#!/bin/bash

#

declare -i sum=0

 

for i in $@; do

if ! id $i &> /dev/null; then

echo “$i is not user”

else

let sum=$sum+$(id -u $i)

fi

done

 

echo “uid sum = $sum”

5、写一个脚本

(1) 传递一些目录给此脚本

(2) 逐个显示每个目录的所有一级文件或子目录的内容类型

(3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型

#!/bin/bash

#

declare -i m=0

declare -i n=0

 

for i in $@;do

if [ -d $i ];then

for x in $i/*;do

echo $x

let m++

if [ -d $x ];then

echo $x

let n++

fi

done

else

echo “$i is not a dir or not file”

fi

 

done

 

echo ” dir = $n   file = $m ”

6、写一个脚本

通过命令行传递一个参数给脚本,参数为用户名

如果用户的id号大于等于500,则显示此用户为普通用户

#!/bin/bash

#

uname=$1

if id $uname &> /dev/null ;then

if [ $(id -u $uname) -ge 500 ];then

echo “this is a normal user”

else

echo “Not an ordinary user”

fi

else

echo “not a user”

fi

7、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来

#!/bin/bash

#

for i in {20..100};do

if ping -c 1 172.16.250.$i &> /dev/null;then

echo “172.16.250.$i is online”

fi

done

8、打印九九乘法表

#!/bin/bash

#

for i in ‘seq 1 9’;do

for j in ‘seq 1 $i’;do

echo -n -e “${j}X${i}=$[$j*$i]\t”

done

echo

done

 

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

(4)
N27_wjxN27_wjx
上一篇 2017-11-15 15:59
下一篇 2017-11-15 19:20

相关推荐

  • 马哥教育网络班22期+第4周课程练习

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost home]# cp -r /etc/skel /home/tuser1 [root@localhost skel]# ch…

    Linux干货 2016-09-05
  • 用户权限

    一、用户和组的主要配置文件
    二、用户管理命令
    三、组管理命令
    四、查看用户相关的ID信息
    五、切换用户或以其他用户身份执行命令

    2018-03-13
  • 系统基础之Btrfs文件系统详解

    btrfs文件系统:技术预览版(centos7) 描述: Btrfs(B-tree,Butter FS,Better fs),GPL授权,Orale,2007 写实复制特性(Cow)     cp –reflink (只能在btrfs文件系统中使用) 想取代ext系统系统, 支…

    Linux干货 2016-09-21
  • Linux基础 & bash相关

    Q1:Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 文件管理类命令: cd, ls, touch, cp, mv, rm, cat, tac, more, less, tail, head; 详细介绍以上命令: cd: 在Linux文件系统上,可以使用切换目录命令cd将shell会话切换到另一个目录。 命令格式: ~]#&nbsp…

    Linux干货 2016-11-06
  • 第四周博客作业

    趁着这几天有时间,先把第四周的作业写了,好在没有什么新的知识点考核。  1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cp -r /etc/skel/ /home/tuser1 […

    Linux干货 2016-12-21
  • BIND 配置DNS服务器以及子域授权

    1、安装bind      [root@www ~]# yum -y install bind 2、查看安装生成的文件      [root@www ~]# rpm -ql bind | less 3、程序路径     /var/named 4、配置…

    Linux干货 2016-03-22

评论列表(1条)

  • 马哥教育
    马哥教育 2017-12-02 09:20

    作业不错,继续加油。