標籤:linux shell 實用小執行個體
判斷目前使用者是否為root管理員,如果是,則執行tar對/etc/目錄執行備份.
#!/bin/bash
# IF current user is root then backup the /etc
if [ "$(id -u)" -eq 0 ];then
tar -czf /root/etc.tar.gz /etc &>/dev/null
else
echo ‘Please switch user on root.‘
exit 9
fi
要求使用者輸入密碼,判斷密碼是否正確.
#!/bin/bash
# Read password and test.
read -p "Enter a password:" password
if [ "$password" == "pass" ];then
echo "OK."
else
echo "Your password is Wrong."
exit 9
fi
九九乘法表
#!/bin/bash
# 九九乘法表
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "$j*$i=$[$i*$j] "
#printf "%-8s" $j*$i=$(($i*$j))
done
echo
done
批量發送郵件
#!/bin/bash
#批量發送郵件
DOMAIN=sharelinux.com
for MAIL_U in tom allen whistle
do
mail -s "Log" [email protected]${DOMAIN} </var/log/message
done
列印網卡設定檔資訊
#!/bin/bash
#Read /etc/sysconfig/network-scripts/ifcfg-eth0 and print out.
LINE=/etc/sysconfig/network-scripts/ifcfg-eth0
while read -r line
do
echo $line
done < $LINE
備份MySQL資料庫
#!/bin/bash
# This scripts is backup mysql database.
DATE=$(date +%Y-%m-%d)
DES=/usr/local/src/DB_MySQL
MYSQL_U="root" #mysql使用者名稱
MYSQL_P="" #mysql密碼,需要修改密碼
MYSQL_H="127.0.0.1" #mysql伺服器IP
if [ ! -d $DES ];then
mkdir -p "$DES"
fi
#擷取資料庫名稱列表
DBS=$(mysql -u${MYSQL_U} -h${MYSQL_H} -Bse ‘show databases‘)
#通過迴圈執行備份所有的MySQL資料庫
for database in $DBS
do
if [ ! "${database}" == "information_schema" ];then
mysqldump -u$MYSQL_U -h$MYSQL_H ${database} |bzip2> "${DES}/${database}_${DATE}_mysql.gz"
#mysqldump -u$MYSQL_U -P$MYSQL_P -h$MYSQL_H $database|bzip2> "${DES}/${database}_${DATE}_mysql.gz"
md5sum ${DES}/${database}_${DATE}_mysql.gz>>${DES}/ReadMe.txt
else
:
fi
done
本文出自 “心靜梵音” 部落格,請務必保留此出處http://mastters.blog.51cto.com/6516495/1532939