bash基础 if elif 多条件判断 for循环

bash基础 if elif 多条件判断 for循环

1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;
     在线的主机使用绿色显示;
     不在线的主使用红色显示;
#!/bin/bash
#filename:testIp.sh
#Author:jian
#Date:2017-10-30
#Discription:
ip=172.16.250.1-172.16.250.254
for x in {1..254};do
ping -c 1 $ip.$x > /dev/null 2>&1 ;
if [ $? -eq 0 ] ; then
echo -e “\033[30;42;2m $ip.$x is UP\033[0m”
else
echo -e “\033[30;41;2m $ip.$x is DOWN\033[0m”
fi
done
2、如何给网络接口配置多个地址,有那些方式;
方式一:修改 /etc/sysconfig/network-scripts/ifcfg-IFCAE 接口文件
DEVICE=eth0
TYPE=Ethernet
UUID=aae15bec-6909-4ab5-bbaf-990ca6f4aa08
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=”System eth0″
IPADDR=192.168.119.138
PREFIX=24
GATEWAY=192.168.119.1
HWADDR=00:0C:29:91:9D:84
IPADDR2=192.168.119.167
PREFIX2=24
GATEWAY2=192.168.119.1
方式二:使用ifconfig添加如ifconfig eth0:0 192.168.119.157 netmask 255.255.255.0(此方式临有效)
方式三:复制ifcfg-IFACE配置文件如 cp ifcfg-eth0 ifcfg-eth0:0 修改 DEVICE=eth0 为DEVICE=eth0:0,修改IPADDR为新增的ip地址。(此方式永久有效)
方式四:使用ip指令添加 ip addr add 192.168.119.135/24 dev ens33。(临时有效)
3、写一个脚本完成以下功能。
(1)假设某目录(/etc/rc.d/rc3.d/)分别有k开头的文件和S卡头的文件若干;
(2)显示所有K开头的文件的文件名,并且给其附加一个stop字符串;
(3)显示所有S开头的文件的文件名,并且给其附加一个start字符串;
(4)显示分别统计K和S文件各有多少。
#!/bin/bash
declare -i i=0;
declare -i j=0;
for x in `ls /etc/rc.d/rc3.d/ |grep “^[S|s]”`
do
echo “$x stop”;
i=$[i+1];
done
for x in `ls /etc/rc.d/rc3.d/ |grep “^[k|K]”`
do
echo “$x start”
j=$[j+1];
done
echo “there are $i files start with S “
echo “there are $j files start with K “
K89rdisc start
K92iptables start
K92pppoe-server start
K95firstboot start
K95rdma start
K99rngd start
there are 39 files start with S
there are 27 files start with K
4、写一个脚本完成以下功能
(1)写一个脚本接收用户名为参数
(2)计算这些用户名id 之和;
#!/bin/bash
#date:2017-10-30
#author:jian
#discription:
if [ $# -lt 2 ];then
echo “please input two userName”;
exit 1;
fi
declare -i uidSum=0;
for name in “$@”
do
uid=`id -u $name`
let uidSum=uidSum+$uid;
done
echo “$uidSum”
5、写一个脚本
(1)传递一些目录给此脚本
(2)逐个显示每个目录的所有一级文件或子目录的内容类型;
(3)统计一共有多个目录;且一共显示了多少个内容文件的类型;
#!/bin/bash
if [ $# -lt 1 ];then
echo “please input director”;
exit 1;
fi
declare -i dSum=0;
declare -i lSum=0;
declare -i xSum=0;
for dir in “$@”
do
for fileName in `ls $dir`
do
if [ -d $dir$fileName ] ;then
echo “$dir$fileName is directory”
let dSum+=1;
elif [ -L $dir$fileName ] ;then
echo “$dir$fileName is Link”
let lSum+=1;
else
echo “$dir$fileName is executable”
let xSum+=1;
fi
done;
echo “$dir”
echo “directory:$dSum”
echo “link:$lSum”
echo “executable:$xSum”
done
bash基础 if elif 多条件判断 for循环

1

6、写一个脚本
(1)通过命令行传递一个参数给脚本,参数为用户名
(2)如果用户的id大于等于500,则显示此用户为普通用户。
#!/bin/bash
if [ $# -ne 1 ];then
echo “please input username”;
exit 1;
fi
id -u $1 > /dev/null 2>&1;
if [ $? -eq 0 ];then
if [ `id -u $1` -gt 500 ];then
echo “$1 is common user”
else
echo “$1 is system user”
fi
else
echo “user is no exit”
fi
[root@centos6 script]# bash user.sh ja
user is no exit
[root@centos6 script]# bash user.sh root
root is system user
[root@centos6 script]# bash user.sh hi
hi is common user
7、写一个脚本,用ping命令测试172.16.250.20-172.16.250.100有那些主机在线,将在线的主机显示出来。
#!/bin/bash
ip=172.16.250.
for x in {20 ..100}
do
ping -c 1 -W 1 $ip$xi > /dev/null 2>&1 ;
if [ $? -eq 0 ];then
echo “$ip$x is up”;
fi
done;
8、打印九九乘法表;
#!/bin/bash
for i in {1..9}
do
for j in {1..9}
do
if [ $i -ge $j ];then
echo -en “${i}*${j}=$(($i*$j)) “
fi
done;
echo -e ” \n “;
done;
2

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

(3)
469008940469008940
上一篇 2017-10-31 10:58
下一篇 2017-10-31 17:32

相关推荐

  • vim

    VIM常用操作命令 模式转换 1、ESC键:编辑模式 --> 一般模式;2、从一般模式 --> 编辑模式 i:在光标所在处的前方转换为编辑模式 a:在光标所在处的后方转换为编辑模式 o:在光标所在行的下方新建一个空行并转换为编辑模式 I:在光标所在行的行首输入 A:在光标所在行的行尾输入 O:在光标所在行的上方新建一个空白行 3、一般模式 --> 末行模式:…

    Linux干货 2017-04-06
  • VMware虚拟机三种连网原理简介

    一、前言         虚拟机(Virtual Machine),在计算机科学中的体系结构里,是指一种特殊的软件,可以在计算机平台和终端用户之间创建一种环境,而终端用户则是基于这个软件所创建的环境来操作系统软件。虚拟机根据它们运用和与直接机器的相关性分为两大类。系统虚拟机和程序虚拟机,系…

    Linux干货 2015-08-03
  • 网络这块一些有趣的感想

    还记的那天老师让我们做两台主机的nexthop就是自己的ip地址,然后互相ping测试能否互相联通的实验,昨晚之后我就在想能不能进行一些改进,因为在我看来这种级别的互相ping的前面一个网络段相同太没意思了。 接着我就尝试了几种IP地址 主机A 主机B 192.0.0.1 192.1.0.1 192.1.1.1 192.2.2.2 192.2.1.1 192…

    Linux干货 2016-09-13
  • socket阻塞与非阻塞,同步与异步、I/O模型

    1. 概念理解      在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:同步:      所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回。也就是必须一件一件事做,等前…

    Linux干货 2015-04-10
  • 马哥教育网络班21期第7周课程练习

    1、创建一个10G分区,并格式为ext4文件系统;    (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;    (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; [root@centos7study&nbs…

    Linux干货 2016-08-29
  • 磁盘分区及lvm管理

    1. 硬盘类型 /dev/sda VS /dev/hda /dev/sda     /dev/sda1     /dev/sda2     /dev/sda3 而又的安装时硬盘驱动设备名为 /dev/hda    /dev/hda1 &nb…

    Linux干货 2016-09-19

评论列表(1条)

  • 马哥教育
    马哥教育 2017-11-15 15:05

    作业写的不错。排版不好。