shell中if控制语句的文件比较

-e 如果文件或者目录存在则为真 [ -e file|dir ]

-f   如果文件存在则为真 [  -f  file  ]

-d    如果目录存在则为真              [  -d  dir  ]

-s    如果文件存在且至少有一个字符则为真 [  -s  file  ]

-r    读权限

-w    写权限

-x    执行权限

#命令行测试

[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 "文件存在且文件存在内容" || echo "文件存在但不存在内容"

文件存在且文件存在内容

[root@web01 /scripts]# [ -s  /etc/passwdd ] && echo "文件存在且文件存在内容" || echo "文件存在但不存在内容"

文件存在但不存在内容

[root@web01 /scripts]# touch  test.txt

[root@web01 /scripts]# [ -s  test.txt  ] && echo "文件存在且文件存在内容" || echo "文件存在但不存在内容"

文件存在但不存在内容

[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 不存在

#备份数据库案例

1. 要有备份目录

2. 备份的数据库名称要带上日期

[root@shell /scripts]# cat back_db.sh

#!/bin/bash

#1.定义变量

Back_Dir=/backup/mysql

M_User=root

M_Pass=123

Date=$(date +%F)

#2.判断数据库的备份目录是否存在

[ -d $Back_Dir ] || mkdir -p $Back_Dir

#3.提示用户输入要备份的数据库名称

read -p "请输入你要备份的数据库名称:" Db

#4.开始备份

mysqldump -u$M_User -p$M_Pass -B $Db >$Back_Dir/${Date}_${Db}.sql

#5.判断数据库是否备份成功

if [ $? -eq 0 ];then

    echo "数据库$Db 备份成功..."

else

    echo "数据库$Db 备份失败..."

fi

[root@shell /scripts]# sh back_db.sh

请输入你要备份的数据库名称:wordpress

数据库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

请输入你要备份的数据库名称:world

数据库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

#严谨及灵活版备份数据库

[root@shell /scripts]# cat back_db.sh

#!/bin/bash

#1.定义变量

Back_Dir=/backup/mysql

#M_User=root

#M_Pass=123

Date=$(date +%F)

#2.判断数据库的备份目录是否存在

[ -d $Back_Dir ] || mkdir -p $Back_Dir

#3.提示用户输入要备份的数据库名称

read -p "请输入你要备份数据库的用户:" M_User

read -s -p "请输入备份数据库用户对应的密码:" M_Pass

echo

read -p "请输入你要备份的数据库名称:" Db

#4.开始备份

mysqldump -u$M_User -p$M_Pass -B $Db >$Back_Dir/${Date}_${Db}.sql

#5.判断数据库是否备份成功

if [ $? -eq 0 ];then

    echo "数据库$Db 备份成功..."

else

    echo "数据库$Db 备份失败..."

fi

[root@shell /scripts]# rm -rf /backup/mysql/

[root@shell /scripts]# sh back_db.sh

请输入你要备份数据库的用户:root

请输入备份数据库用户对应的密码:

请输入你要备份的数据库名称:wordpress

数据库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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容