1. 寫一個腳本,使用ping命令探測172.16.250.1-172.15.250.254之間所有主機的在線狀態;在線的主機使用綠色顯示;不在線的主機使用紅色顯示
“`
#!/bin/bash
#
for i in {1..254}; do
ping 172.16.250.$i -w 1 -c 1 &> /dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo -e “\033[32mHost 172.16.250.$i is online.\033[0m ”
else
echo -e “\033[31mHost 172.16.250.$i is offline.\033[0m ”
fi
done
“`
2. 如何給網絡配置多個地址,有哪些方式?
“`
(1) 通過ip命令添加
~]# ip addr add <IP_ADDR>/<MASKLEN> dev <IFACE_NAME>
(2) 針對CentOS7,使用nmtui命令添加
(3) 使用ifconfig命令
~]# ifconfig IFACE_LABEL IPADDR/NETMASK
(4) 可以通過復制原有配置文件到新文件IFACE:0,然后修改IP地址,網卡別名不支持動態獲取地址
“`
3. 寫一個腳本,完成以下功能
(1) 假設某目錄(/etc/rc.d/rc3.d)下分別有K開頭的文件和S開頭的文件若干;
(2) 顯示所有以K開頭的文件的文件名,并且給其附加一個stop字符串;
(3) 顯示所有以S開頭的文件的文件名,并且給其附加一個start字符串;
(4) 分別統計S開頭和K開頭的文件各有多少?
“`
#!/bin/bash
#
declare -i numk=0
declare -i nums=0
for files in `ls /etc/rc.d/rc3.d`; do
if [[ $files =~ ^K[[:digit:]].* ]]; then
echo “$files stop.”
let numk+=1
elif [[ $files =~ ^S[[:digit:]].* ]]; then
echo “$files start.”
let nums+=1
fi
done
echo “The number for Kfiles is $numk.”
echo “The number for Sfiles is $nums.”
“`
4. 寫一個腳本,完成以下功能
(1) 腳本能接受用戶名作為參數;
(2) 計算此些用戶的ID之和;
“`
#!/bin/bash
#
echo -e “This is a script for calculate the summation for the user UID you entered, you can enter as many times as you want until you enter quit, and the script will calculate the summation for all these users’ UID for you. \n”
read -p “Please enter a username: ” user
declare -i sum=0
until [ $user == ‘quit’ ]; do
if id $user &> /dev/null; then
declare -i UserID=`id -u $user`
let sum+=$UserID
read -p “Please reenter a username: ” user
else
echo “User $user does not exist.”
read -p “Please reenter a username: ” user
fi
done
echo “The summation of UID for all the users you entered is $sum.”
“`
5. 寫一個腳本
(1) 傳遞一些目錄給此腳本;
(2) 逐個顯示每個目錄的所有一級文件或子目錄的內容類型;
(3) 統計一共有多少個目錄,且一共顯示了多少個文件的內容類型;
“`
#!/bin/bash
#
[ -e /tmp/filetype.txt ] && rm -f /tmp/filetype.txt
read -p “Please enter a directory: ” dirc
declare -i typenum=0
declare -i dircnum=0
until ls $dirc &> /dev/null; do
echo “The directory you entered does not exist.”
read -p “Please enter a directory: ” dirc
done
touch /tmp/filetype.txt
until [ $dirc == ‘quit’ ]; do
let dircnum+=1
for dir_file in `ls $dirc`; do
file_type=`file -b $dirc/$dir_file | cut -d’,’ -f1`
echo $file_type | grep “symbolic link” &> /dev/null && file_type=’symbolic link’
cat /tmp/filetype.txt | grep “$file_type” &> /dev/null
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo $file_type >> /tmp/filetype.txt
let typenum+=1
fi
done
read -p “Please enter a directory: ” dirc
done
echo “You totally entered $dircnum directories.”
echo “There are $typenum types of file in the directories you entered.”
rm -f /tmp/filetype.txt
“`
6. 寫一個腳本
(1) 通過命令行傳遞一個參數給腳本,參數為用戶名;
(2) 如果用戶的id號大于等于500,則顯示此用戶為普通用戶;
“`
#!/bin/bash
#
read -p “Please enter a user: ” user
until [ $user == ‘quit’ ]; do
if ! id $user &> /dev/null; then
echo “The user you entered doesn’t exist.”
read -p “Please enter a user: ” user
else
declare -i UserID=`id -u $user`
if [ $UserID -ge 500 ]; then
echo “User $user is a common user.”
fi
read -p “Please enter a user: ” user
fi
done
“`
7. 寫一個腳本,用ping命令測試172.16.250.20-172.16.250.100之間有哪些主機在線,將在線的顯示出來;
“`
#!/bin/bash
#
for i in {20..100};do
ping -c 1 -W 1 172.16.250.$i &> /dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo “Host 172.16.250.$i is online.”
fi
done
“`
8. 打印九九乘法表;
for循環
“`
#!/bin/bash
#
for a in {1..9}; do
for b in `seq 1 $a`; do
echo -n -e “$X${a}=$[$*${a}]\t”
done
echo
done
“`
while循環
“`
#!/bin/bash
#
declare -i a=1
while [ $a -le 9 ]; do
declare -i b=1
while [ $b -le $a ]; do
echo -n -e “$X${a}=$[$*${a}]\t”
let b++
done
let a++
echo
done
“`
until循環
“`
#!/bin/bash
declare -i a=1
until [ $a -gt 9 ]; do
declare -i b=1
until [ $b -gt $a ]; do
echo -n -e “$X${a}=$[$*${a}]\t”
let b++
done
let a++
echo
done
“`
9. 打印逆序的九九乘法表;
for循環
“`
#!/bin/bash
#
for a in `seq 1 9 | sort -r`; do
for b in `seq 1 $a | sort -r`; do
echo -n -e “$X${a}=$[$*${a}]\t”
done
echo
done
“`
while循環
“`
#!/bin/bash
#
declare -i a=9
while [ $a -ge 1 ]; do
declare -i b=$a
while [ $b -ge 1 ]; do
echo -n -e “$X${a}=$[$*${a}]\t”
let b–
done
let a–
echo
done
“`
until循環
“`
#!/bin/bash
#
declare -i a=9
until [ $a -lt 1 ]; do
declare -i b=$a
until [ $b -lt 1 ]; do
echo -n -e “$X${a}=$[$*${a}]\t”
let b–
done
let a–
echo
done
“`
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/89523
腳本沒有問題,建議排版可以截圖下來。不然不好分析,也不好看。