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

相关推荐

  • 使用pyenv管理不同版本的python

    安装: 安装: $ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash 在你的shellrc文件中添加: export PATH=”$HOME/.pyenv/bin:$PATH” eval “$(pyenv init …

    Linux干货 2015-03-12
  • varnish的基本配置

    主程序:varnish 配置文件: ·/etc/varnish/varnish.params—配置varnish服务进程的工作特性,例如监听的地址和端口,缓存机制; ·/etc/varnish/default.vcl—配置各Child/Cache线程的缓存工作属性; VCL状态引擎: 内建变量: ·req.*:request,表示由客…

    2017-08-08
  • 用户管理

    http://www.jianshu.com/p/a07ae29ca345

    Linux干货 2017-07-23
  • 第七周 系统启动维护管理

    1、简述linux操作系统启动流程 2、简述grub启动引导程序配置及命令行接口详解 3、实现kickstart文件制作与光盘镜像制作    

    2018-01-12
  • netfilter/iptables 基础入门

    netfilter/iptables 基础入门 Firewall防火墙的实现方式 什么是netfilter?        Netfilter是由Linux内核提供的框架,允许以定制处理程序的形式实现各种与网络相关的操作。Netfilter为包过滤,网络地址转换和端口转换提供各种功能和操作,它们提供了通过网络…

    2017-06-13
  • 第一周课程练习

    1、描述计算机的组成及其功能。 计算机由硬件、操作系统、软件三大部分组成。 硬件包括核心CPU(大脑处理中心)、必备电源(心脏动力来源)、硬盘(仓库)内存(中转站)、主板(协调)、网卡、声卡、显卡、风扇、光驱、显示器、鼠标、键盘、麦克风音箱,闪存、蓝牙等。  计算机五大组成部分及功能。   运算器:     …

    Linux干货 2016-08-15

评论列表(1条)

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

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