例1:
#!/bin/bash
sum=0;
for i in
dolet "sum+=i"
done
echo "the sum is $sum"
例2:不知道迴圈次數,批量解壓縮
#!/bin/bash
cd /root
ls *.sh > ls.log
y=1for i in $(cat ls.log)
doecho $y
y=$(( $y + 1 ))
done
例3:知道解壓縮
#!/bin/bash
s=0for(( i=1;i<=100;i=i+1 ))
dos=$(( $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 ))
douseradd $name$i &>/dev/null
echo $pass | passwd --stdin $ "$name$i" &>/dev/null
done
fifi---------------
格式為 if then fi 。if後有空格,【】 !前有空格 -z之前有空格
例5:設計乙個shell程式,新增乙個新組為class1,然後新增屬於這個組的30個使用者,使用者名稱的形式為stdxx,其中xx從01到30。
#!/bin/bash
groupadd class1
for ((i=1;i<=30;i++))
doif [ $i -lt 10 ];then
username="std0"$i
else
username="std"$i
fiuseradd -g class1 $username
done
例6:向指令碼傳遞引數
指令碼內容:vim xjbcdcs
#!/bin/bash
echo "helloworld"
echo $?
echo $@
echo $#
-------------------------
./xjbcdcs a b c
結果:helloworld
0a 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++))
dofor((j=1;j<=i;j++))
dolet "cheng=i*j"
printf "$i*$j=$cheng"
if [[ "$cheng" -gt 9 ]]
then
printf " "
else
printf " "
fidone
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
例十:監控指令碼
直接貼指令碼:
1、cpu
#!/bin/bash
currentdate=`date -d today '+%y%m%d'`
currenttime=`date -d today '+%y%m%d%h%m'`
mytext="$currenttime\t`top -b -n 1 | grep cpu\(s\)`"
echo -e $mytext >> /home/www/monitor/log/cpu$currentdate.log
2、memo
#!/bin/bash
currentdate=`date -d today '+%y%m%d'`
currenttime=`date -d today '+%y%m%d%h%m'`
mytext="$currenttime\t`top -b -n 1 | grep mem:`"
echo -e $mytext >> /home/www/monitor/log/memo$currentdate.log
3、io
#!/bin/bash
currentdate=`date -d today '+%y%m%d'`
currenttime=`date -d today '+%y%m%d%h%m'`
mytext="$currenttime\t`iostat -p sda | grep -w sda`"
echo -e $mytext >> /home/www/monitor/log/io$currentdate.log
4、swap
#!/bin/bash
currentdate=`date -d today '+%y%m%d'`
currenttime=`date -d today '+%y%m%d%h%m'`
mytext="$currenttime\t`top -b -n 1 | grep swap:`"
echo -e $mytext >> /home/www/monitor/log/swap$currentdate.log
shell指令碼例項
1.批量建立10個系統賬號test01 test10,並隨機設定8位數密碼 bin bash for i in seq w 10 do useradd test i echo random madsum cut c 8 tee a passwd.txt stdin test i done 2.在目錄...
Shell 指令碼例項
指令碼內容如下 bin bash action 定義函式,進行操作指南 action 1 database mysql uroot p 1 en e show databases grep e schema v mkdir p mnt sqldump e database name sql case...
Shell指令碼例項
1.寫乙個指令碼,利用迴圈計算10的階乘 bin sh factorial 1 for a in seq 1 10 dofactorial expr factorial a done echo 10 factorial 注 上面有一行,for a in seq 1 10 其中seq 1 10 即列出...