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
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;
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/88173
评论列表(1条)
作业写的不错。排版不好。