-e 如果文件或者目錄存在則為真 [ -e file|dir ]
-f ? 如果文件存在則為真 [? -f? file? ]
-d? ? 如果目錄存在則為真? ? ? ? ? ? ? [? -d? dir? ]
-s? ? 如果文件存在且至少有一個字符則為真 [? -s? file? ]
-r? ? 讀權(quán)限
-w? ? 寫權(quán)限
-x? ? 執(zhí)行權(quán)限
#命令行測試
[root@web01 /scripts]# [ -e? /scripts ] && echo "目錄或者文件存在" || echo "目錄或者文件不存在"
目錄或者文件存在
[root@web01 /scripts]# [ -e? /script ] && echo "目錄或者文件存在" || echo "目錄或者文件不存在"
目錄或者文件不存在
[root@web01 /scripts]# [ -e? /etc/passwd ] && echo "目錄或者文件存在" || echo "目錄或者文件不存在"
目錄或者文件存在
[root@web01 /scripts]# [ -f? /etc/passwd ] && echo "文件存在" || echo "文件不存在"
文件存在
[root@web01 /scripts]# [ -f? /etc/passwdd ] && echo "文件存在" || echo "文件不存在"
文件不存在
[root@web01 /scripts]# [ -d? /etc/passwd ] && echo "文件存在" || echo "文件不存在"
文件不存在
[root@web01 /scripts]# [ -d? /etc ] && echo "文件存在" || echo "文件不存在"
文件存在
[root@web01 /scripts]# [ -d? /etc ] && echo "目錄存在" || echo "目錄不存在"
目錄存在
[root@web01 /scripts]# [ -s? /etc/passwd ] && echo "文件存在且文件存在內(nèi)容" || echo "文件存在但不存在內(nèi)容"
文件存在且文件存在內(nèi)容
[root@web01 /scripts]# [ -s? /etc/passwdd ] && echo "文件存在且文件存在內(nèi)容" || echo "文件存在但不存在內(nèi)容"
文件存在但不存在內(nèi)容
[root@web01 /scripts]# touch? test.txt
[root@web01 /scripts]# [ -s? test.txt? ] && echo "文件存在且文件存在內(nèi)容" || echo "文件存在但不存在內(nèi)容"
文件存在但不存在內(nèi)容
[root@web01 /scripts]# cat test.txt
#腳本測試
[root@web01 /scripts]# cat if-6.sh
#!/bin/bash
if [ -f $1 ];then
? ? echo "文件 $1 存在"
else
? ? echo "文件 $1 不存在 "
fi
[root@web01 /scripts]#
[root@web01 /scripts]#
[root@web01 /scripts]# sh if-6.sh /etc/hosts
文件 /etc/hosts 存在
[root@web01 /scripts]# sh if-6.sh /etc/host
文件 /etc/host 不存在
#備份數(shù)據(jù)庫案例
1. 要有備份目錄
2. 備份的數(shù)據(jù)庫名稱要帶上日期
[root@shell /scripts]# cat back_db.sh
#!/bin/bash
#1.定義變量
Back_Dir=/backup/mysql
M_User=root
M_Pass=123
Date=$(date +%F)
#2.判斷數(shù)據(jù)庫的備份目錄是否存在
[ -d $Back_Dir ] || mkdir -p $Back_Dir
#3.提示用戶輸入要備份的數(shù)據(jù)庫名稱
read -p "請輸入你要備份的數(shù)據(jù)庫名稱:" Db
#4.開始備份
mysqldump -u$M_User -p$M_Pass -B $Db >$Back_Dir/${Date}_${Db}.sql
#5.判斷數(shù)據(jù)庫是否備份成功
if [ $? -eq 0 ];then
? ? echo "數(shù)據(jù)庫$Db 備份成功..."
else
? ? echo "數(shù)據(jù)庫$Db 備份失敗..."
fi
[root@shell /scripts]# sh back_db.sh
請輸入你要備份的數(shù)據(jù)庫名稱:wordpress
數(shù)據(jù)庫wordpress 備份成功...
[root@shell /scripts]# ll /backup/mysql/
total 452
-rw-r--r-- 1 root root 462530 2020-02-15 14:20 2020-02-15_wordpress.sql
[root@shell /scripts]# sh back_db.sh
請輸入你要備份的數(shù)據(jù)庫名稱:world
數(shù)據(jù)庫world 備份成功...
[root@shell /scripts]# ll /backup/mysql/
total 692
-rw-r--r-- 1 root root 462530 2020-02-15 14:20 2020-02-15_wordpress.sql
-rw-r--r-- 1 root root 244162 2020-02-15 14:21 2020-02-15_world.sql
#嚴(yán)謹(jǐn)及靈活版?zhèn)浞輸?shù)據(jù)庫
[root@shell /scripts]# cat back_db.sh
#!/bin/bash
#1.定義變量
Back_Dir=/backup/mysql
#M_User=root
#M_Pass=123
Date=$(date +%F)
#2.判斷數(shù)據(jù)庫的備份目錄是否存在
[ -d $Back_Dir ] || mkdir -p $Back_Dir
#3.提示用戶輸入要備份的數(shù)據(jù)庫名稱
read -p "請輸入你要備份數(shù)據(jù)庫的用戶:" M_User
read -s -p "請輸入備份數(shù)據(jù)庫用戶對應(yīng)的密碼:" M_Pass
echo
read -p "請輸入你要備份的數(shù)據(jù)庫名稱:" Db
#4.開始備份
mysqldump -u$M_User -p$M_Pass -B $Db >$Back_Dir/${Date}_${Db}.sql
#5.判斷數(shù)據(jù)庫是否備份成功
if [ $? -eq 0 ];then
? ? echo "數(shù)據(jù)庫$Db 備份成功..."
else
? ? echo "數(shù)據(jù)庫$Db 備份失敗..."
fi
[root@shell /scripts]# rm -rf /backup/mysql/
[root@shell /scripts]# sh back_db.sh
請輸入你要備份數(shù)據(jù)庫的用戶:root
請輸入備份數(shù)據(jù)庫用戶對應(yīng)的密碼:
請輸入你要備份的數(shù)據(jù)庫名稱:wordpress
數(shù)據(jù)庫wordpress 備份成功...
[root@shell /scripts]# ll /backup/mysql/
total 452
-rw-r--r-- 1 root root 462530 2020-02-15 14:28 2020-02-15_wordpress.sql