vi /vim編輯器
命令基礎(100多個)
網絡服務(基礎)nfs、sync、 inotify、 lamp、 sersync、 ssh key批量分發管理
#!/bin/bash
#author by xiaojiang
#print hello world
echo 'hello world\!'
變量 賦值
#!/bin/bash
#define path variables
#AUTH XIAOJAING 2016
A=123
echo "$A"
echo $UID
echo $PWD
echo "?"
echo "#"
echo "*"
#!/bin/bash
#by authors xj 2016
echo -e "\033[32mPlease select Menu follow:\033[1m"
echo "1)安裝apache服務器"
echo "2)安裝Mysql服務器"
echo "3)安裝PHP服務器"
echo "4)配置LAMP服務器"
echo "------------------------"
shell 編程if條件語句 if for while case
if 條件判斷語句
if (表達式) #
語句一
else
語句二
fi
eg:
#!/bin/bash
NUM1=100
NUM2=200
if(($Num1>$NUM2)); then
echo "this $NUM1 greater $NUM2 !"
else
echo "this $NUM1 little $NUM2 !"
fi
邏輯運算符
-f 判斷文件是否存在 if [-f filename]
-d 判斷目錄是否存在 if [-d dir]
-eg 等於 應用於:整型比較
-ne 不等於 應用於:整型比較
-lt 小於 應用於:整型比較
-gt 大於 應用於:整型比較
-le 小於等於 應用於:整型比較
-ge 大於等於 應用於:整型比較
-a 雙方都成立 (and) 邏輯表達式 -a 邏輯表達式
-o 單方成立 (or) 邏輯表達式 -o 邏輯表達式
-z 空字符串
判斷目錄是否存在
eg1:
#!/bin/bash
#auth if test
#by authors xj 2016
if [ ! -d /tmp/20161210 ];then
mkdir -p /tmp/20161210
echo -4 "\033[032mThis dir is create successful!\033[0m"
else
echo "\033[32mThis dir is exist,please exit.\033[0m"
fi
eg2:
#!/bin/bash
FILES=/tmp/test.txt
if [ ! -f $FILES];then
echo "ok">>$FILES
else
echo -e "033[32m---------------\033[1m"
cat $FILES
fi
多個條件測試判斷
eg3:
#!/bin/bash
scores=80
if [[ $scores -gt 85 ]];then
echo "very good!";
elif [[ $scores -gt 75 ]];then
echo "good!";
elif [[ $scores -gt 65 ]];then
echo "pass!";
else
echo "no pass!"
fi
if條件語句寫mysql備份腳本
#!bin/bash
#backup Mysql
#autor xj 20161210
#define Path 定義變量
#必須使用root $UID爲系統變量
BAK_DIR=/data/backup/`date +%Y%m%d`
MYSQLDB=discuz
MYSQLUSR=backup
MYSQLPW=123456
MYSQLCMD=/usr/bin/mysqldump
if [$UID -ne 0];then
echo "must to be use root for exec shell"
exit
if [ ! -d $BAK_DIR];then
mkdir -p $BAK_DIR
echo "\033[32mThe $BAK_DIR create successful!\033[0m"
else
echo "This $BAK_DIR is exists---"
fi
MYSQL backup command
mysqldump -ubackup -p123456 -d discus>$BAK_DIR/$MYSQLDB.sql
if [$? -eq 0 ];then
echo "\033[32mThe Mysql Backup $MYSQLDB SUCCESSFULLY!\033[0m"
else
echo "\033[32mThe Mysql Backup $MYSQLDB fAILED,please checked.\033[0m"
if 條件綜合shell 實戰腳本編寫LAMP
//一鍵源碼安裝LAMP
1)安裝apache服務器
2)安裝Mysql服務器
3)安裝PHP服務器
4)配置LAMP服務器並啓動服務
//
1.apach 安裝部署
下載http-2.2.27.tar.gz 下載url 解壓 進入安裝目錄,configure ;make ;make install
2.mysql服務器的安裝
下載mysql-5.5.20.tar.gz 下載url 解壓 進入安裝目錄 configure ;make ;make install
3.php服務器的安裝
下載php-8.3.8.tar.gz 下載url 解壓 進入安裝目錄 configure ;make ;make install
4.lamp架構的整合和服務啓動
/usr/local/apache2/bin/apachectl2 start
vi htdocs/index.php
<?php
phpinfo();
?>
//
#/bin/bash
# auto make install lamp
#by xj
#httpd define path variables
H_FILES=http-2.2.27.tar.bz2
H_FILES_DIR=httpd-2.2.27
H_URL=http://mirros.cnnic.cn/apache/httpd/
H_PREFIX=/usr/local/apache2/
if [ -z "$1" ];then
echo -e "\033[32mPlease select Menu follow:\033[0m"
echo -e "\033[32m1)編譯安裝apache服務器\033[1m"
echo "2)編譯安裝Mysql服務器"
echo "3)編譯安裝PHP服務器"
echo "4)配置LAMP服務器"
echo -e "\033[31musage:{/bin/bash $0 1|2|3|4|help}\033[0m"
exit
fi
if [ "$1" eq "1" ];then
wget -c $H_URL/H_FILES &&tar -jxvf $H_FILES && cd H_FILES_DIR;./configure --prefix=$H_PREFIX
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe H_FILES_DIR Server Install successful!\033[0m"
else
echo -e "\033[32mThe H_FILES_DIR Server Install Failed!,please checked\033[0m"
exit
fi
fi