shell示例总结。
示例1:编写一个操作文件的脚本,如下所示:
***1.backup **
***2.copy **
***3.quit **
input a choice:
代码:
#!/bin/bash
cat<<eof
*******************
***1.backup **
***2.copy **
***3.quit **
*******************
eof
read -p "input a choice:" ot
case $ot in
1|backup)
echo "backup....."
;;
2|copy)
echo "copy....."
;;
3|quit)
exit
;;
*)
echo "please input 1-3|backup|copy|quit"
esac
示例2 :编写一个启动 apache 服务脚本
代码:
#!/bin/bash
case $1 in
start)
/usr/bin/systemctl $1 httpd
/usr/bin/ps aux |grep httpd >/dev/null 2>&1
echo "httpd start"
;;
stop)
/usr/bin/systemctl $1 httpd
/usr/bin/ps aux |grep httpd >/dev/null 2>&1
echo "httpd stop"
;;
restart)
echo "httpd stop"
/usr/bin/systemctl stop httpd
/usr/bin/ps aux |grep httpd >/dev/null 2>&1
echo "httpd start"
/usr/bin/systemctl start httpd
/usr/bin/ps aux |grep httpd >/dev/null 2>&1
;;
status)
/usr/bin/systemctl $1 httpd
/usr/bin/ps aux |grep httpd >/dev/null 2>&1
echo "httpd status"
;;
*)
echo "please input start|stop|restart|status"
esac
示例3:#指定以\n 回车作为分隔符输出/etc/hosts
代码:
#!/bin/bash
IFS=$'\n'
list=`cat /etc/hosts`
for i in $list
do
echo "$i"
done
示例4:降序输出 10 到 1 的数字
代码:
#!/bin/bash
var=10
while [ $var -gt 0 ]
do
echo "$var"
var=$[$var-1]
done
示例5:实现以下效果
1 * 1 = 1
2 * 2 = 4
3 * 3 = 9
4 * 4 = 16
5 * 5 = 25
6 * 6 = 36
7 * 7 = 49
8 * 8 = 64
9 * 9 = 81
代码:
#!/bin/bash
num=1
while [ $num -lt 10 ]
do
sum=$[ $num * $num ]
echo "$num * $num = $sum"
num=$[ $num + 1]
done
示例6:批量添加 a.txt 文件 5 个用户
代码:
#!/bin/bash
list=`cat /root/test/a.txt`
for i in $list
do
id $i &>/dev/null
if [ $? -ne 0 ];then
useradd -M $i
echo "12345"|passwd --sdin $i &>/dev/null
echo "$i is created"
else
echo "$i is exist"
fi
done
示例7:批量创建帐号并生成随机密码
代码:
#!/bin/bash
[ -f /tmp/passwd.txt ]||touch /tmp/passwd.txt
for i in a{1..10}
do
useradd $i
pass=$(date +%s | md5sum |cut -c 1-8)
sleep 1
echo $i:$pass >>/tmp/passwd.txt
echo $pass|passwd --stdin $i
if [ $? -eq 0 ];then
echo "user $i create successfully"
else
echo "user $i create failly"
fi
done
示例8:打印九九乘法表
代码:
#!/bin/bash
for i in `seq 9`
do
for j in `seq $i`
do
echo -n "$i*$j=`echo $[$i * $j]` "
done
echo " "
done
示例9:监控httpd服务自动启动并记录
代码:
#!/bin/bash
while true
do
port=$(nmap -sT 192.168.130.63| grep tcp | grep -w http |awk '{print $2}')
if [ $port == "open" ];then
echo "$(date) httpd service is opening!">>/tmp/autostart-acc.txt
else
/usr/bin/systemctl start httpd &>/dev/null
echo "$(date) httpd service restart!">>/tmp/autostart-err.txt
fi
sleep 60
done
注:
nmap -sT TCP connect()扫描,这种方式会在目标主机的日志中记录大批的连接请求以及错误信息
grep -w 精确匹配
示例10:将/opt 目录下所有的日志文件全自动打包
代码:
#!/bin/bash
DEC_DIR=/opt/`date +%Y-%m-%d`
SRC_DIR=/opt
logname=$(find $SRC_DIR -name *.log)
if [ ! -d $DEC_DIR ];then
mkdir -p $DEC_DIR
fi
cd $SRC_DIR
for i in $logname
do
tar -zcf opt.tar.gz $i
done
mv $SRC_DIR/*.tar.gz $DEC_DIR
ls $DEC_DIR>/dev/null 2>&1
echo "the scripts exec end,file back successfully!"
示例11:找出 192.168.130.1-10 网段中,服务器已经关机的 IP 地址
#!/bin/bash
for i in `seq 10`
do
ping -c 192.168.130.$i>/dev/null 2>&1
if [ $? -ne 0 ];then
echo "192.168.130.$i is shutdown"
fi
done