本身写了一下小的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