vim /root/7.sh
#!/bin/bash
X=$[RANDOM%10+1]
read -p "[1-10]你猜:" cai
if [ $cai -eq $X ];then
echo 恭喜你猜对了
exit
elif [ $num -gt $X ];then
echo 猜大了
else
echo 太小了
fi
vim /root/6.sh
#!/bin/bash
read -p "请输入一个软件包:" b
b=$(rpm -qa | grep "$a")
if [[ -z $b ]];then
echo "没有安装"
else
echo "已经安装"
fi
vim /root/5.sh
#!/bin/bash
netstat -anptu | grep httpd &> /dev/null
if [ $? -eq 0 ];then
echo "已经启动"
else
echo "没有启动"
fi
vim /root/4.sh
#!/bin/bash
ping -c3 -i0.2 -w1 $1 &> /dev/null
if [ $? -eq 0 ];then
echo "host $1 up"
else
echo "host $1 down"
fi
vim /root/3.sh
#!/bin/bash
[ -d /mnt ] || mkdir /mnt
mount /dev/cdrom /mnt
vim /root/2.sh
#!/bin/bash
[ ! -d /mnt ] && mkdir /mnt
mount /dev/cdrom /mnt
vim /root/1.sh
#!/bin/bash
num=$(who | wc -l)
[ $num -gt 10 ] && mail -s errot root < mail.txt
crontab -e
* * * * * /root/1.sh
vim /mail.txt
隔壁老王来了
ping 176.130.8.50-150,开启的有多少个,没有开启的有多少个
#!/bin/bash
x=0
y=0
for i in {100..150}
do
ping -c2 -i0.1 -W1 176.130.8.$i &> /dev/null
if [ $? -eq 0 ];then
echo "176.130.8.$i is up"
let x++
else
echo "176.130.8.$i is down"
let y++
fi
done
echo "开机的有$x个, 关机的有$y个"
求1+2+3+4+5......100的和
#!/bin/bash
sum=0
for i in {1..100}
do
let sum+=i
done
echo "总和$sum"
求1*2*3*4......30的积
#!/bin/bash
sum=1
for i in {1..50}
do
let sum\*=i
done
echo "总共是 $sum"
将/var/log每个文件,单独打包,保存到/tmp中
for i in `ls /var/log`
do
tar -czf /tmp/$i.tar.gz /var/log/$i
done
----------辅助理解/var/log/文件理解的打包过程
for i in a.txt b.txt c.txt d.txt
do
i=a.txt
tar -czf /tmp/$i.tar.gz /var/log/$i
done
利用Shell脚本,猜一个随机数
#!/bin/bash
y=0
num=$[RANDOM%100+1]
while :
do
read -p "[1-100] , 你猜一个数:" cai
let y++
if [ $cai -eq $num ];then
echo "恭喜猜对了, 你猜了$y次"
exit
elif [ $cai -gt $num ];then
echo "猜大了"
else
echo "猜小了"
fi
done
case语句
如果参数是redhat,则显示是fedora
如过参数是fedora,则显示是redhat
如果参数是其他,则显示是Useage
#!/bin/bash
case "$1" in
redhat)
echo "fedora";;
fedora)
echo "redhat";;
*)
echo "Usage";;
esac
case语句(判断语句),简化if语句-----------只能进行两个值之间是否相等
#!/bin/bash
case "$1" in
-n)
touch $2;;
-e)
vim $2;;
-c)
cat $2;;
-r)
rm -rf $2;;
*)
echo "Usage:$0 (-n|-e|-c|-r|)" 文件名
esac
Shell调用函数----------就是给一段命令取个别名(函数)
基本格式:函数名 (){
大括号中间跟命令
}
调用函数:函数名
ll(ls -l)
#!/bin/bash
cecho (){
echo -e "\033[$1m$2\033[0m"
}
cecho 32 OK
cecho 34 Error
cecho 35 Waring
3X:是字体颜色
4X:是背景颜色
X:表示样式
While语句
固定格式:while
i=1
while [ $i -lt 10 ]
do
echo "你好"
let i++
done
break,contine,exit给脚本的间接中断在重复使用
(1)break中断整个循环体
(2)continue中断本次循环,立即跳入下一次循环
(3)exit中断整个脚本
#!/bin/bash
for i in {1..5}
do
[ $i -eq 3 ] && break
echo $i
done
echo OK
#!/bin/bash
for i in {1..5}
do
[ $i -eq 3 ] && continue
echo $i
done
echo OK
#!/bin/bash
for i in {1..5}
do
[ $i -eq 3 ] && exit
echo $i
done
echo OK
#!/bin/bash
sum=0
while :
do
read -p "请输入数字[0结束]:" xx
[ $xx -eq 0 ] && break
let sum+=xx
done
echo "总和是$sum"
打印9*9乘法表
#!/bin/bash
for i in `seq 9`
do
for x in `seq $i`
do
echo -n "$i*$x=$[$i*$x]"
done
echo
done
猴子摘香蕉
#!/bin/bash
x=1
for i in {1..8}
do
let x=x+1
let x=x*2
done
echo $x
国际象棋
#!/bin/bash
b=1
while [ $b -le 4 ]
do
for i in {1..4}
do
echo -en "\033[40m \033[0m"
echo -en "\033[47m \033[0m"
done
echo
for i in {1..4}
do
echo -en "\033[47m \033[0m"
echo -en "\033[40m \033[0m"
done
echo
if [ $b -eq 2 ];then
echo -e "\033[47m 楚河汉界 \033[0m"
fi
let b++
done
#!/bin/bash
read -p "请输入用户名:" name
read -p "请输入用户密码:" pass
if [ -z "$name" ];then
echo "必须输入用户名"
exit
fi
pass=${pass:-123456}
useradd $name
echo "$pass" | passwd --stdin $name
提示用户输入一个数:如果输入的是10,则脚本1+2+3+4.....10
如果输入的是20,则脚本1+2+3....+20
如过没有输入,则脚本1+2+3+4......+100
#!/bin/bash
read -p "请输入一个数字:" xx
xx=${xx:-100}
sum=0
for i in `seq $xx`
do
let sum+=i
done
echo "总和是$sum"
创建文件touch {a,b,c,d}.txt,把 .txt后缀改为 .doc
#!/bin/bash
for i in `ls *.txt`
do
mv $i ${i%.txt}.txt
done
改变参数,赋予变量
#!/bin/bash
for i in `ls *.$1`
do
mv $i ${i%.$1}.$2
done
#!/bin/bash
sum=0
for i in `seq 2 2 100`
do
let sum=sum+i
done
echo "$sum"
sum=0
for i in `seq 1 2 99`
do
let sum+=i
done
echo "总和是$sum"
#tmp=0
read -p "shu zi" num
num=${num:-100}
for i in `seq 2 2 $num`
do
let tmp+=i
done
echo $tmp
#!/bin/bash
case $1 in
start)
if [ -f /usr/local/nginx/logs/nginx.pid ];then
echo "正在运行"
else
/usr/local/nginx/sbin/nginx
echo "启动成功"
fi;;
stop)
if [ ! -f /usr/local/nginx/logs/nginx.pid ];then
echo "已经关闭"
else
/usr/local/nginx/sbin/nginx -s stop
echo "关闭成功"
fi;;
restart)
if [ -f /usr/local/nginx/logs/nginx.pid ];then
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
echo "重启成功"
else
/usr/local/nginx/sbin/nginx
echo "重启成功"
fi;;
status)
if [ -f /usr/local/nginx/logs/nginx.pid ];then
netstat -ntulp |grep nginx
else
echo "没有运行"
fi;;
*)
echo "Error";;
esac