本身寫了一下小的shell實例,雖然很小,但全部的大的程序都是由小的模塊堆積起來的,程序員必定要懂得一種腳本的書寫,而我,只會在linux下工做,因此就只能寫linux的shell腳本了,呵呵,本文會陸續更新,給本身加油!html
1.模擬linux登錄shellmysql
vim linux1.sh
#!/bin/bash
echo -n "login:"
read name
echo -n "password:"
read passwd
if [ $name = "tianqi" -a $passwd = "abc123" ];then
echo "the host and password is right!"
else echo "input is error!"
fi
sh linux1.sh
login:tianqi
password:abc123
the host and password is right!
[ec2-user@ip-172-31-21-107 shell]$jquery
友情連接:天天寫點shell——read的用法linux
友情連接:解析shell腳本中if語句的用法程序員
2.比較兩個數的大小
vim number1.shsql
#!/bin/bash
echo "please enter two number"
read a
read b
if test $a -eq $b
then echo "NO.1 = NO.2"
elif test $a -gt $b
then echo "NO.1 > NO.2"
else echo "NO.1 < NO.2"
fishell
sh number1.sh
please enter two number
5
6
NO.1 < NO.2
[root@tianqi-01 shell]# 數據庫
3.查找/root/目錄下是否存在該文件vim
vim root1.shbash
#!/bin/bash
echo "enter a file name:"
read a
if test -e /root/$a
then
echo "the file is exist!"
else
echo "the file is not exist!"
fi
sh root1.sh
enter a file name:
1.txt
the file is exist!
[root@tianqi-01 shell]#
4.for循環的使用
vim for1.sh
#!/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
echo "$num"
done
sh for1.sh
1
2
3
4
5
6
7
8
9
10
[root@tianqi-01 shell]#
5.查看是否當前用戶
vim user1.sh
#!/bin/bash
echo "Please enter a user:"
read a
b=$(whoami)
if test $a = $b
then
echo "The user is running"
else
echo "The user is not running"
fi
sh user1.sh
Please enter a user:
root
The user is running
[root@localhost shell]#
6.mysql自動備份腳本
#!/bin/bash
#auto backup mysql shell
#tianqi 2018-07-20
#自動備份數據庫,並添加任務計劃腳本
#定義變量=========================
bak_cmd=/usr/local/mysql/bin/mysqldump
bak_host=localhost
bak_db=tianqi
bak_user=root
bak_pass="123456"
bak_dir=/tmp/mysqlbackup/
bak_date=`date +%F`
if [ $UID -ne 0 ]
then
echo "必須使用ROOT用戶才能執行此腳本"
exit
fi
if [ ! -d $bak_dir ]
then
mkdir $bak_dir
echo "這個目錄建立成功"
else
echo "這個目錄已經存在"
fi
#正式備份
$bak_cmd -u$bak_user -p$bak_pass -h$bak_host -d $bak_db > $bak_dir/tianqi_$bak_date.sql
if [ $? -eq 0 ]
then
echo "數據庫備份成功"
echo "備份目錄:$bak_dir"
else
echo "數據庫備份出錯,請檢查"
fi
#自動刪除30天之前的備份數據
find $bak_dir/ -mtime +30 |xargs rm -rf {} \;
if [ $? -eq 0 ]
then
echo "刪除30天之前的備份數據成功"
else
echo "命令執行出錯,請檢查"
fi
#建立自動備份計劃任務
grep "back_mysql" /var/spool/cron/root >> /dev/null
if [ $? -ne 0 ]
then
echo "30 20 * * 5 /root/shell/backup_mysql > /tmp/mysql.log 2>&1" >> /var/spool/cron/root
fi
7.使用shell腳本監控CPU使用率(使用vmstat工具)
#!/bin/bash
if [ `uname` != "Linux" ]
then
echo "system not linux"
exit 1
fi
which vmstat &> /dev/null
if [ $? != 0 ]
then
echo "vmstat command not found, please install package procps"
exit
fi
cpu_us=`vmstat |sed -n '$'p |awk '{print $13}'`
cpu_sy=`vmstat |sed -n '$'p |awk '{print $14}'`
cpu_id=`vmstat |sed -n '$'p |awk '{print $15}'`
cpu_wa=`vmstat |sed -n '$'p |awk '{print $16}'`
cpu_sum=$[$cpu_us+$cpu_sy]
cpuinfo()
{
CPU_SUM=$cpu_sum%($CPU_USER=$cpu_us% + $CPU_SYSTEM=$cpu_sy%)
CPU_IDLE=$cpu_id%
CPU_WAIT=$cpu_wa
}
cpuinfo
if [ $cpu_sum -gt 90 ]
then
echo "CPU utilization $cpu_sum" |mail -s "CPU Monitor" 123@qq.com
fi
8.添加10個用戶,從user101-user110
1)for用法
#!/bin/bash
for i in `seq 101 110`
do
useradd user$i
echo "user$i" |passwd user$i --stdin &> /dev/null
done
2)while用法
#!/bin/bash
i=101
while [ $i -le 110 ]
do
useradd user$i
echo "user$i" |passwd user$i --stdin &> /dev/null
done
3)until用法
#!/bin/bash
i=101
until [ $i -gt 110 ]
do
useradd user$i
echo "user$i" |passwd user$i --stdin &> /dev/null
done
9.求100之內的偶數和奇數之和
1)for的寫法
#!/bin/bash
for i in `seq 0 100`
do
if [ $[i%2] = 0 ]
then
let sum+=$i
else [ $[i%2] = 1 ]
let sum1+=$i
fi
done
echo $sum
echo $sum1
2)while的寫法
#!/bin/bash
i=0
while [ $i -le 100 ]
do
if [ $[i%2] = 0 ]
then
let sum+=$i
else [ $[i%2] = 1 ]
let sum1+=$i
fi
let i++
done
echo $sum
echo $sum1
3)until的寫法
#!/bin/bash
i=0
until [ $i -gt 100 ]
do
if [ $[i%2] = 0 ]
then
let sum+=$i
else [ $[i%2] = 1 ]
let sum1+=$i
fi
let i++
done
echo $sum
echo $sum1
10.打印九九乘法表
1)for的寫法
#!/bin/bash
for i in `seq 1 9`
do
for j in `seq 1 $i`
do
echo -n -e "${j}X${i}=$[$j*$i]\t"
done
echo
done
2)while的寫法
#!/bin/bash
i=1
while [ $i -le 9 ]
do
j=1
while [ $j -le $i ]
do
echo -n -e "${j}X${i}=$[$j*$i]\t"
let j++
done
echo
let i++
done
3)until的寫法
#!/bin/bash
i=1
until [ $i -gt 9 ]
do
j=1
until [ $j -gt $i ]
do
echo -n -e "${j}X${i}=$[$j*$i]\t"
let j++
done
echo
let i++
done
11.打印逆序九九乘法表
1)for的寫法
#!/bin/bash
for ((i=9;i>=1;i--))
do
for ((j=i;j<=9;j++))
do
echo -n -e "${i}"X"${j}=$[$j*$i]\t"
done
echo
done
2)while的寫法
#!/bin/bash
i=9
while [ $i -ge 1 ]
do
j=$i
while [ $j -le 9 ]
do
echo -n -e "${i}"X"${j}=$[$j*$i]\t"
let j++
done
echo
let i--
done
3)until的寫法
#!/bin/bash
i=9
until [ $i -lt 1 ]
do
j=$i
until [ $j -gt 9 ]
do
echo -n -e "${i}"X"${j}=$[$j*$i]\t"
let j++
done
echo
let i--
done
友情連接:阿銘Linux