例1:
#!/bin/bash
sum=0;
for i in {1..100..2}
do
let "sum+=i"
done
echo "the sum is $sum"
例2:不知道循环次数,批量解压缩
#!/bin/bash
cd /root
ls *.sh > ls.log
y=1
for i in $(cat ls.log)
do
echo $y
y=$(( $y + 1 ))
done
例3:知道解压缩
#!/bin/bash
s=0
for(( i=1;i<=100;i=i+1 ))
do
s=$(( $s +i ))
echo "1jiadao100 is $s"
done
例4:批量添加用户
#!/bin/bash
read -t 30 -p "input name" name
read -t 30 -p "input num" num
read -t 30 -p "input pass" pass
if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
then
y=$(echo $num|sed 's/^[0-9]*$'//g)
if [ -z "$y" ]
then
for (( i=1;i<$num;i=i+1 ))
do
useradd $name$i &>/dev/null
echo $pass | passwd --stdin $ "$name$i" &>/dev/null
done
fi
fi---------------
格式为 if then fi 。if后有空格,【】 !前有空格 -z之前有空格
例5:
设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。
#!/bin/bash
groupadd class1
for ((i=1;i<=30;i++))
do
if [ $i -lt 10 ];then
username="std0"$i
else
username="std"$i
fi
useradd -G class1 $username
done
例6:向脚本传递参数
脚本内容:vim xjbcdcs
--------------------------
#!/bin/bash
echo "helloworld"
echo $?
echo $@
echo $#
-------------------------
./xjbcdcs a b c
结果:
helloworld
0
a b c
3
例7:for循环 里面有shell命令行
输出文件名
#! /bin/bash
#使用ls命令的执行结果作为列表
for file in $(ls)
do
#输出每个文件名
echo "$file"
done
例8:九九乘法表
#!/bin/bash
for ((i=1;i<=9;i++))
do
for((j=1;j<=i;j++))
do
let "cheng=i*j"
printf "$i*$j=$cheng"
if [[ "$cheng" -gt 9 ]]
then
printf " "
else
printf " "
fi
done
echo
done
-------------------
注:用expr 如何实现呢。
[root@localhost ~]# expr 10/2
10/2
[root@localhost ~]# expr 10 / 2
5
[root@localhost ~]# expr 10 * 2
expr: syntax error
[root@localhost ~]# expr 10 * 2
20
用let计算好像更方便快捷一些
[root@localhost ~]# let a=5-1
[root@localhost ~]# echo $a
4
[root@localhost ~]# let a=8*9
[root@localhost ~]# echo $a
72
例9:备份mysql数据库(本例稍改动,只打包某个目录)
#!/bin/bash
date=$(date +%y%m%d)
size=$(du -sh /etc)
if [ -d /tmp/dbback ];then
echo "Date is :$date" > /tmp/dbback/db.txt
echo "size is :$size" >>/tmp/dbback/db.txt
cd /tmp/dbback
tar -zcf etc_$date.tar.gz /etc db.txt &>/dev/null
rm -rf /tmp/dbback/db.txt
else
mkdir /tmp/dbback
echo "Date is :$date" > /tmp/dbback/db.txt
echo "size is :$size" >>/tmp/dbback/db.txt
cd /tmp/dbback
tar -zcf etc_$date.tar.gz /etc db.txt &>/dev/null
rm -rf /tmp/dbback/db.txt
例十:监控脚本
linux下监控cpu、memo、io、swap性能数据
1、cpu
CurrentDate=`date -d today '+%Y%m%d'`
CurrentTime=`date -d today '+%Y%m%d%H%M'`
mytext="$CurrentTime `top -b -n 1 | grep Cpu(s)`"
echo -e $mytext >> /home/www/monitor/log/cpu$CurrentDate.log
CurrentDate=`date -d today '+%Y%m%d'`
CurrentTime=`date -d today '+%Y%m%d%H%M'`
mytext="$CurrentTime `top -b -n 1 | grep Mem:`"
echo -e $mytext >> /home/www/monitor/log/memo$CurrentDate.log
CurrentDate=`date -d today '+%Y%m%d'`
CurrentTime=`date -d today '+%Y%m%d%H%M'`
mytext="$CurrentTime `iostat -p sda | grep -w sda`"
echo -e $mytext >> /home/www/monitor/log/io$CurrentDate.log
CurrentDate=`date -d today '+%Y%m%d'`
CurrentTime=`date -d today '+%Y%m%d%H%M'`
mytext="$CurrentTime `top -b -n 1 | grep Swap:`"
echo -e $mytext >> /home/www/monitor/log/swap$CurrentDate.log