1、寫一個腳本,使用ping命令探測172.16.250.1-172.16.250.254之間的所有主機的在線狀態;
在線的主機使用綠色顯示;
不在線的主使用紅色顯示;
#!/bin/bash
#
for i in 172.16.250.{1..254};do
if ping -c 6 $i &> /dev/null; then
echo -e “\e[1;31m $i \e[0m”
else
echo -e “\e[1;32m $i \e[0m”
fi
done
2、如何給網絡接口配置多個地址,有哪些方式?
(1) ip addr add IFADDR dev IFACE
(2) ifconfig IFACE_LABEL IPADDR/NETMASK
示例:
[root@localhost yum.repos.d]# ip addr show eth1
4: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:0c:29:ca:38:36 brd ff:ff:ff:ff:ff:ff
[root@localhost yum.repos.d]# ip addr add 192.168.200.66/24 dev eth1
[root@localhost yum.repos.d]# ip addr add 192.168.200.99/24 dev eth1
[root@localhost yum.repos.d]# ifconfig eth1:0 192.168.200.132/24
[root@localhost yum.repos.d]# ip addr show dev eth1
4: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:0c:29:ca:38:36 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.66/24 scope global eth1
inet 192.168.200.99/24 scope global secondary eth1
inet 192.168.200.132/24 brd 192.168.200.255 scope global secondary eth1:0
(3)編輯配置文件/etc/sysconfig/network-scripts/ifcfg-IFACE
注意:使用命令配置會立即有效,但不會永久有效。
編輯完配置文件,需要重啟網絡服務,才會立即生效
3、寫一個腳本,完成以下功能
(1) 假設某目錄(/etc/rc.d/rc3.d/)下分別有K開頭的文件和S開頭的文件若干;
(2) 顯示所有以K開頭的文件的文件名,并且給其附加一個stop字符串;
(3) 顯示所有以S開頭的文件的文件名,并且給其附加一個start字符串;
(4) 分別統計S開頭和K開頭的文件各有多少;
#!/bin/bash
#
a=0
b=0
for i in $(ls /tmp/rc3.d) ;do
echo $i > /tmp/1.txt
if [ $(grep -o “^K” /tmp/1.txt ) == “K” ] &> /dev/null;then
echo ${i}stop
let a++
elif [ $(grep -o “^S” /tmp/1.txt ) == “S” ] &> /dev/null;then
echo ${i}start
let b++
else
echo $i
fi
done
rm -rf /tmp/1.txt
echo “The number of files at the beginning of the K is: $a”
echo “The number of files at the beginning of the S is: $b”
4、寫一個腳本,完成以下功能
(1) 腳本能接受用戶名作為參數;
(2) 計算此些用戶的ID之和;
#!/bin/bash
#
if [ $# -lt 2 ];then
echo “User name is at least two.”
exit 2
fi
a=0
for i in $*;do
if ! id $i &> /dev/null;then
echo “$i is not exists.”
exit 3
else
for j in $(id -u $i);do
j=$(($j+$a))
done
a=$j
fi
done
echo “The sum of $* id is: $a”
5、寫一個腳本
(1) 傳遞一些目錄給此腳本;
(2) 逐個顯示每個目錄的所有一級文件或子目錄的內容類型;
(3) 統計一共有多少個目錄;且一共顯示了多少個文件的內容類型;
#!/bin/bash
#
if [ $# -lt 1 ];then
echo “Directory numbers wrong.”
exit 2
else
for i in $*;do
if ! [ -d $i ];then
echo “$i is not directory.”
exit 3
fi
done
fi
dir=$(pwd)
a=0
x=0;xx=0;xxx=0;xxxx=0;xxxxx=0;xxxxxx=0;xxxxxxx=0
for j in $*;do
cd $j
for i in $(ls);do
if [ -f $i ];then
echo “$i is common file.”
x=1
elif [ -d $i ];then
echo “$i is directory.”
let a++
xx=1
elif [ -L $i ];then
echo “$i is link file.”
xxx=1
elif [ -b $i ];then
echo “$i is block file.”
xxxx=1
elif [ -c $i ];then
echo “$i is character file.”
xxxxx=1
elif [ -p $i ];then
echo “$i is pipeline file.”
xxxxxx=1
elif [ -S $i ];then
echo “$i is socket file.”
xxxxxxx=1
else
echo “$i Unkown.”
fi
done
done
cd $dir
echo “Directory:$a”
echo “Type:$(($x+$xx+$xxx$xxxx+$xxxxx+$xxxxxx+$xxxxxxx))”
6、寫一個腳本
通過命令行傳遞一個參數給腳本,參數為用戶名
如果用戶的id號大于等于500,則顯示此用戶為普通用戶;
#!/bin/bash
#
#!/bin/bash
#
if ! [ $# -le 1 ] ;then
echo “arguments is wrong.”
exit 2
elif ! id $1 &> /dev/null;then
echo “user is not exists.”
exit 3
else
a=$(id -u $1)
fi
if [ $a -ge 500 ];then
echo “$1 is general user. ”
else
echo “$1 is system user or root”
fi
7、寫一腳本,用ping命令測試172.16.250.20-172.16.250.100以內有哪些主機在線,將在線的顯示出來;
#!/bin/bash
#
for i in 172.16.250.{20..100};do
if ping -c 6 $i &> /dev/null; then
echo $i
done
8、打印九九乘法表;
#!/bin/bash
for i in {1..9};do
for j in $(seq 1 $i );do
echo -n -e “$j*$i=$(($i*$j))\t ”
done
echo
done
i=1
while [ $i -le 9 ];do
for j in $(seq 1 $i);do
echo -n -e “$j*$i=$(($i*$j))\t ”
done
echo
let i++
done
原創文章,作者:N26-xiaocong,如若轉載,請注明出處:http://www.www58058.com/76783
如果有執行結果效果會更好,加油。