if [條件1]; then
表達式1
elif [條件2];then
表達式2
else
表達式2
fi
if 判斷目錄、檔案
#/bin/bash
#auto testfile
#by authors guoqi 2020
DIR=/tmp/20200714
if [ ! -d $DIR ];then
mkdir -p $DIR
echo"this $DIR creat success!"
else
echo -e "\033[32mthis $DIR is exist,please exit!\033[0m"
fi
FILES=/tmp/testfile.txt
if [ ! -f $FILES ];then
echo "OK" >> $FILES
else
echo -e "\033[32m-------------------\033[1m"
cat $FILES
fi
#/bin/bash
#auto test switch use if esle
#author by guoqi
#scores=90
scores=$1
# -z 判斷輸入是否爲空
if [ -z $scores ];then
echo "usage:{$0 60|80}"
exit
fi
# gt >
if [[ $scores -gt 85 ]];then
echo "very good"
elif [[ $scores -gt 75 ]];then
echo "good"
elif [[ $scores -gt 60 ]];then
echo "you pass"
else echo "no pass"
fi
while [條件]:
do
表達式
done
#!/bin/bash
# 指令碼生成一個 100 以內的亂數,提示使用者猜數位,根據使用者的輸入,提示使用者猜對了,
# 猜小了或猜大了,直至使用者猜對指令碼結束。
# RANDOM 爲系統自帶的系統變數,值爲 0‐32767的亂數
# 使用取餘演算法將亂數變爲 1‐100 的亂數
num=$[RANDOM%100+1]
echo "$num"
# 使用 read 提示使用者猜數位
# 使用 if 判斷使用者猜數位的大小關係:‐eq(等於),‐ne(不等於),‐gt(大於),‐ge(大於等於),
# ‐lt(小於),‐le(小於等於)
while :
do
read -p "計算機生成了一個 1‐100 的亂數,你猜: " cai
if [ $cai -eq $num ]
then
echo "恭喜,猜對了"
exit
elif [ $cai -gt $num ]
then
echo "Oops,猜大了"
else
echo "Oops,猜小了"
fi
done
#!/bin/bash
# 實時監控本機記憶體和硬碟剩餘空間,剩餘記憶體小於500M、根分割區剩餘空間小於1000M時,發送報警郵件給root管理員
# 提取根分割區剩餘空間
disk_size=$(df / | awk '/\//{print $4}')
# 提取記憶體剩餘空間
mem_size=$(free | awk '/Mem/{print $4}')
while :
do
# 注意記憶體和磁碟提取的空間大小都是以 Kb 爲單位
if [ $disk_size -le 512000 -a $mem_size -le 1024000 ]
then
mail ‐s "Warning" root <<EOF
Insufficient resources,資源不足
EOF
fi
done
for 變數 int 回圈列表
do
表達式1
done
#!/bin/bash
# 9*9 乘法表(編寫 shell 指令碼,列印 9*9 乘法表)
for i in `seq 9`
do
for j in `seq $i`
do
echo -n "$j*$i=$[i*j] "
done
echo
done
#!/bin/bash
# 一鍵部署 LNMP(原始碼安裝版本)
menu()
{
clear
echo " ##############‐‐‐‐Menu‐‐‐‐##############"
echo "# 1. Install Nginx"
echo "# 2. Install MySQL"
echo "# 3. Install PHP"
echo "# 4. Exit Program"
echo " ########################################"
}
choice()
{
read -p "Please choice a menu[1‐9]:" select
}
install_nginx()
{
id nginx &>/dev/null
if [ $? -ne 0 ];then
useradd -s /sbin/nologin nginx
fi
if [ -f nginx‐1.8.0.tar.gz ];then
tar -xf nginx‐1.8.0.tar.gz
cd nginx‐1.8.0
yum -y install gcc pcre‐devel openssl‐devel zlib‐devel make
./configure ‐‐prefix=/usr/local/nginx ‐‐with‐http_ssl_module
make
make install
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
cd ..
else
echo "沒有 Nginx 原始碼包"
fi
}
install_mysql()
{
yum -y install gcc gcc‐c++ cmake ncurses‐devel perl
id mysql &>/dev/null
if [ $? -ne 0 ];then
useradd -s /sbin/nologin mysql
fi
if [ -f mysql‐5.6.25.tar.gz ];then
tar -xf mysql‐5.6.25.tar.gz
cd mysql‐5.6.25
cmake .
make
make install
/usr/local/mysql/scripts/mysql_install_db ‐‐user=mysql ‐‐datadir=/usr/local/mysql/data/
‐‐basedir=/usr/local/mysql/
chown -R root.mysql /usr/local/mysql
chown -R mysql /usr/local/mysql/data
/bin/cp -f /usr/local/mysql/support‐files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/bin/cp -f /usr/local/mysql/support‐files/my‐default.cnf /etc/my.cnf
echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf
ldconfig
echo 'PATH=\$PATH:/usr/local/mysql/bin/' >> /etc/profile
export PATH
else
echo "沒有 mysql 原始碼包"
exit
fi
}
install_php()
{
#安裝 php 時沒有指定啓動哪些模組功能,如果的使用者可以根據實際情況自行新增額外功能如‐‐with‐gd 等
yum -y install gcc libxml2‐devel
if [ -f mhash‐0.9.9.9.tar.gz ];then
tar -xf mhash‐0.9.9.9.tar.gz
cd mhash‐0.9.9.9
./configure
make
make install
cd ..
if [ ! ‐f /usr/lib/libmhash.so ];then
ln -s /usr/local/lib/libmhash.so /usr/lib/
fi
ldconfig
else
echo "沒有 mhash 原始碼包檔案"
exit
fi
if [ -f libmcrypt‐2.5.8.tar.gz ];then
tar -xf libmcrypt‐2.5.8.tar.gz
cd libmcrypt‐2.5.8
./configure
make
make install
cd ..
if [ ! -f /usr/lib/libmcrypt.so ];then
ln -s /usr/local/lib/libmcrypt.so /usr/lib/
fi
ldconfig
else
echo "沒有 libmcrypt 原始碼包檔案"
exit
fi
if [ -f php‐5.4.24.tar.gz ];then
tar -xf php‐5.4.24.tar.gz
cd php‐5.4.24
./configure ‐‐prefix=/usr/local/php5 ‐‐with‐mysql=/usr/local/mysql ‐‐enable‐fpm ‐‐
enable‐mbstring ‐‐with‐mcrypt ‐‐with‐mhash ‐‐with‐config‐file‐path=/usr/local/php5/etc ‐‐with‐
mysqli=/usr/local/mysql/bin/mysql_config
make && make install
/bin/cp -f php.ini‐production /usr/local/php5/etc/php.ini
/bin/cp -f /usr/local/php5/etc/php‐fpm.conf.default /usr/local/php5/etc/php‐fpm.conf
cd ..
else
echo "沒有 php 原始碼包檔案"
exit
fi
}
while :
do
menu
choice
case $select in
1)
install_nginx
;;
2)
install_mysql
;;
3)
install_php
;;
4)
exit
;;
*)
echo Sorry!
esac
done