1.数据库备份
- 将数据库备份到/mnt/mysqldump
- 当备份文件存在时询问
- 选择 S)跳过 B)备份 O)跳过
[root@141 MY_SHELL]# cat dbdump.sh
#!/bin/bash
DB_MESSAGE=`mysql -uroot -EN -e "show databases;"| grep -E "^\*|schema$ -v"`
####规则显示数据库名
mkdir -p /mnt/mysqldump ##新建备份目录
for DB_NAME in ${DB_MESSAGE}
do
if
[ -e "/mnt/mysqldump/${DB_NAME}.mysql" ]
##检测数据库备份文件是否存在
then
echo "${DB_NAME}.mysql exists"
read -p "INSTRUCTION: " p
case $p in
s) ##用户选择跳过
echo skip~~~
exit 0
;;
o) ##覆盖数据库文件
mysqldump -uroot ${DB_MESSAGE}> /mnt/mysqldump/${DB_NAME}.sql
echo Overwrite
;;
b) ##备份文件
mysqldump -uroot $DB_MESSAGE > /mnt/mysqldump/${DB_NAME}.sql
echo backup
;;
*) ##其余显示错误
echo error
;;
esac
else ##其余情况均直接备份
mysqldump -uroot $DB_MESSAGE > /mnt/mysqldump/${DB_NAME}.sql
[ "$?" -eq "0" ]&&{
echo "$DB_NAME is backped"
}
fi
done
2.时钟时秒倒计时
[root@141 mnt]# cat TIMER.sh
#!/bin/bash
for NUM in {0..9}
do
echo "$((10 - $NUM))"
sleep 1
done
设定从1:10开始倒计时,计时结束后输出提示并结束。
[root@141 ~]# cat timer.sh
#!/bin/bash:
MIN=1 #赋初值
for ((SEC=10;SEC>=0;SEC--))
do
while [ "$SEC" -eq "0" ] #设定SEC=0-->59
do
echo -ne "\r$MIN:$SEC\r" #输出时间并清屏
if
[ "$MIN" -eq "0" ]
then
echo finished
exit 0
else
sleep 1
echo -ne "\r$MIN:$SEC\r"
SEC=59
MIN=$(($MIN-1))
fi
break
done
echo -n "$MIN:$SEC "
sleep 1 #休眠1s
echo -ne "\r \r"
done
检验成功
[root@141 ~]# sh timer.sh
0:55
[root@141 ~]# sh timer.sh
finished
倒计时完整精简版
#!/bin/bash
read -p "Hour:" h
read -p "Minute:" m
read -p "Second:" s
p=$[h*3600+m*60+s]
for ((i=$p;i>0;i--))
do
H=$[$i/3600]
M=$[$i/60%60]
S=$[$i%60]
echo -ne "\r$H:$M:$S\r"
sleep 1
done
3.计算器
[root@141 mnt]# cat Calculator.sh
#!/bin/bash
Continue(){
read -p "Continue...?y/n" CMD
case $CMD in
y)
Cal
;;
n)
exit 0
;;
*)
Continue
;;
esac
}
Cal(){
read -p "Number:" a
read -p "Number:" b
read -p "Operation:" OPR
case $OPR in
+)
echo $(($a+$b))
;;
-)
echo $(($a-$b))
;;
*)
echo $(($a*$b))
;;
/)
echo $(($a/$b))
;;
%)
echo $(($a%$b))
;;
*)
error
;;
esac
Continue
}
Cal
4.批量添加用户
#!/bin/bash
check_file(){
if
[ "$#" -ne "2" ]
then
echo "File does not match"
exit 0
elif
[ ! -e "$2" ]||[ ! -e "$1" ]
then
echo "File does not exist"
exit 0
fi
}
check_line(){
USER_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
PASS_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
if
[ "$USER_LINE" -ne "$PASS_LINE" ]
then
echo ERROR_LINE
exit 0
fi
}
useradd(){
PASSLINE=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
for LINE_NUM in `seq 1 $PASSLINE`
do
USERNAME=`sed -n "${LINE_NUM}p" $2`
PASSWORD=`sed -n "${LINE_NUM}p" $3`
useradd $USERNAME
[ "$?" -eq "0" ]&&{
echo $PASSWORD | passwd --stdin $USERNAME&>/dev/null && echo $USERNAME exists
}
done
}
case $1 in
add)
check_file $2 $3
check_line $3 $3
useradd $2
;;
*)
;;
esac
5.判断文件存在或输出文件属性
#!/bin/bash
if
[ "$#" -ne "1" ]
then
echo ERROR
fi
for i in f b L d S
do
if
[ -"$i" "$1" ]
then
case $i in
f)
echo "$1 exists and is a regular file"
;;
b)
echo "$1 exists and is block special"
;;
L)
echo "$1 exists and is a symbolic link"
;;
d)
echo "$1 exists and is a directory"
;;
S)
echo "$1 exists and is a socket"
;;
esac
fi
done
检验:
[root@141 mnt]# sh checkfile.sh /mnt/
/mnt/ exists and is a directory
[root@141 mnt]# sh checkfile.sh /dev/vda
/dev/vda exists and is block special
[root@141 mnt]# sh checkfile.sh /etc/system-release
/etc/system-release exists and is a regular file
/etc/system-release exists and is a symbolic link
6.检测可以连接的主机,并使用ssh连接获取hostname
function testIp {
for((i=$1;i<=$2;i++))
do
ip=$3.$i
ping -c1 -w1 $ip &>/dev/null
#ping网段内主机,根据命令执行返回值来确定下一步的动作
if [ $? -eq 0 ]
then
result=`/root/useradd/answer.exp $ip redhat hostname | grep foundation | cut -d . -f 1-3`
#执行自动应答脚本根据命令执行结果处理输出
echo $result | grep foundation &>/dev/null
if [ $? -ne 0 ]
then
echo "ssh connection refused by host $ip"
else
echo "$ip's hostname is: $result"
fi
else
echo "$ip can not be reached!"
fi
done
}
print_usage
read -p "Please input startip endip and zone: " ipstart ipend ipzone
#函数调用
testIp $ipstart $ipend $ipzone
7.单行或者多行字符反转
#!/bin/bash
for arg in $@
do
inputstr=$arg
len=${#inputstr}
for((i=1;i<=$len;i++))
do
echo -n ${inputstr:0-$i:1}
#字符从右切片,每次左移一位取一个字符
done
echo ""
done
[root@141 mnt]# sh reve.sh agdjkvklf
flkvkjdga