shell-3 课堂笔记
=====================================================================================
exam-1、批量添加用户
[root@localhost ~]# vi user.txt
liuye
dengchao
sunli
zhangjie
xiena
[root@localhost ~]# vi test1.sh
#!/bin/bash
#批量添加用户
USERLIST=$(cat /root/user.txt)
for users in $USERLIST; do
useradd $users
echo "123456" |passwd --stdin $users &> /dev/null
done
=====================================================================================
exam-2、根据IP地址检查主机状态
[root@localhost ~]# vi ping.sh
#!/bin/bash
#主机存活检查
>tong.txt
>butong.txt
for ipaddr in 192.168.6.{1..20}; do
ping -c 2 -i 0.1 -W 1 $ipaddr &> /dev/null
if [ $? -eq 0 ]; then
echo "主机 $ipaddr 存活" |tee -a tong.txt
else
echo "主机 $ipaddr 不通" |tee -a butong.txt
fi
done
echo "**************************************************"
echo "存活的主机有 $(cat tong.txt |wc -l) 个"
echo "它们分别是: "
awk '{print $2}' tong.txt
echo "不通的主机有 $(cat butong.txt |wc -l) 个"
echo "它们分别是: "
awk '{print $2}' butong.txt
======================================================================================
exam-3、
#!/bin/bash
#添加以stu开头的20个用户
pre="stu"
i=1
while [ $i -le 20 ]; do
useradd ${pre}$i
echo "123456" |passwd --stdin ${pre}$i &> /dev/null
let i++
done
=========================================================================================
exam-4、
[root@localhost ~]# cat pricegame.sh
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo "商品实际价格为0-999之间,猜猜看是多少?"
while true
do
read -p "请输入你猜测的价格数目:" INT
let TIMES++
if [ $INT -eq $PRICE ] ; then
echo "恭喜你答对了,实际价格是 $PRICE"
echo "你总共猜测了 $TIMES 次"
exit 0
elif [ $INT -gt $PRICE ] ; then
echo "太高了!"
else
echo "太低了!"
fi
done
========================================================================================
exam-5
[root@localhost ~]# cat aa.sh
#!/bin/bash
# remove ln
for lnfile in $(ls -l /opt |grep "^l" |awk '{print $8}') ; do
rm -rf /opt/$lnfile
done
====================================================================================
exam-6
[root@localhost ~]# cat hitkey.sh
#!/bin/bash
read -p "请输入一个字符,并按Enter键确认:" KEY
case "$KEY" in
[a-z]|[A-Z])
echo "您输入的是 字母。"
;;
[0-9])
echo "您输入的是 数字。"
;;
*)
echo "您输入的是 空格、功能键或其他控制字符。"
esac
=====================================================================================
exam-7
[root@localhost ~]# cat test3.sh
#!/bin/bash
#菜单设计
caishuzi() {
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo "商品实际价格为0-999之间,猜猜看是多少?"
while true
do
read -p "请输入你猜测的价格数目:" INT
let TIMES++
if [ $INT -eq $PRICE ] ; then
echo "恭喜你答对了,实际价格是 $PRICE"
echo "你总共猜测了 $TIMES 次"
break
elif [ $INT -gt $PRICE ] ; then
echo "太高了!"
else
echo "太低了!"
fi
done
}
suanshu() {
while :
do
A=$RANDOM
B=$RANDOM
echo "请计算下面的加法试题"
read -p "$A + $B = " res
da=$(expr $A + $B)
if [ $res -eq "$da" ]; then
echo "恭喜你答对了,答案是: $da"
sleep 5
break
else
echo "很抱歉,请继续"
fi
done
}
aaa() {
clear
cat <<MADAY
--------------------------------------------
/ 教育改变生活 \\
| 1 ) 猜数字游戏 |
| 2 ) 算术 |
| q ) 退出 |
---------------------------------------------
MADAY
read -p "请输入您的选项(1|2|q): " ky
case $ky in
1)
caishuzi
;;
2)
suanshu
;;
q|Q)
exit 2
;;
*)
echo "您的输入有误,请重新输入"
;;
esac
}
while true
do
aaa
done
======================================================================================
exam-8: 启停脚本架构
[root@localhost ~]# cat /etc/init.d/newd
#!/bin/bash
# chkconfig: - 80 15
# description: aaaaaaaaaaaaaaaaaaaaaaaa
case $1 in
start)
echo "启动成功"
;;
stop)
echo "停止成功"
;;
restart)
$0 stop
$0 start
;;
*)
echo "用法:$0(start|stop|restart)"
exit 1
;;
esac
===================================================================================
exam-9
需求描述
为已安装的zebra程序编写 zebrad 服务控制脚本
启动服务:/etc/init.d/zebrad start
终止服务:/etc/init.d/zebrad start
重启服务:/etc/init.d/zebrad restart
查看服务状态:/etc/init.d/zebrad status
未正确指定“start”、“stop”、“restart”、“status”参数时,输出用法帮助信息后退出
在启动、终止服务时应显示相关提示信息
将zebrad添加为系统服务
-------------------------------------------------------------------------------------
实现思路
参考已有的系统脚本建立 zebrad 脚本文件
将启动、终止zebra程序等执行操作分别定义为函数
通过“/usr/local/sbin/zebra -d”命令启动zebra服务
结合“/var/run/zebra.pid”文件中的PID号终止zebra进程
通过判断“zebra -d”进程信息确定zebra服务状态
设置case分支语句
读取执行脚本时提供的未知参数“$1”,并进行比较
若为“start”、“stop”等字串,分别调用对应函数
否则执行默认操作,显示用法帮助信息并退出
执行“chkconfig --add zebrad”添加为系统服务
-------------------------------------------------------------------------------------
[root@localhost ~]# cd /usr/src/zebra-0.95a/
[root@localhost zebra-0.95a]# ./configure --prefix=/usr/local/zebra ; make ; make install
[root@localhost zebra-0.95a]# ln -s /usr/local/zebra/sbin/* /usr/local/sbin/
[root@localhost zebra-0.95a]# cd /usr/local/zebra/etc/
[root@localhost etc]# cp zebra.conf.sample zebra.conf
脚本文件开头要保留用于chkconfig配置的内容,例如:
#!/bin/bash
# chkconfig: 2345 99 60
# description: Start/Stop the zebra router daemon.
START()
{
if [ -e /var/run/zebra.pid ]; then
echo "为 zebrad 启动 zebrad: [失败]"
exit 1
else
/usr/local/sbin/zebra -d &> /dev/null
if [ -e var/run/zebra.pid ]; then
echo "为 zebrad 启动 zebrad: [确定]"
else
echo "为 zebrad 启动 zebrad: [失败]"
fi
fi
}
STOP()
{
if [ -e /var/run/zebra.pid ]; then
PID=$(cat /var/run/zebra.pid)
kill -9 $PID
rm -rf /var/run/zebra.pid
if [ -e /var/run/zebra.pid ]; then
echo "关闭 zebrad: [失败]"
exit 2
else
echo "关闭 zebrad: [确定]"
fi
else
echo "关闭 zebrad: [失败]"
exit 2
fi
}
STATUS()
{
if [ -e /var/run/zebra.pid ]; then
pid=$(cat /var/run/zebra.pid)
echo "zebrad (pid $pid) 正在运行..."
exit 0
else
echo "zebrad 已停"
exit 1
fi
}
case $1 in
start)
START
;;
stop)
STOP
;;
restart)
$0 stop
$0 start
;;
status)
STATUS
;;
*)
echo "用法: $0 {start|stop|restart|status}"
exit 1
;;
esac