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 “${b}X${a}=$[${b}*${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 “${b}X${a}=$[${b}*${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 “${b}X${a}=$[${b}*${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 “${b}X${a}=$[${b}*${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 “${b}X${a}=$[${b}*${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 “${b}X${a}=$[${b}*${a}]\t”
let b–
done
let a–
echo
done
“`
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/89523
评论列表(1条)
脚本没有问题,建议排版可以截图下来。不然不好分析,也不好看。