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.www58058.com/88173
作業寫的不錯。排版不好。