Linux 判断文件是否存在的shell脚本代码
发表于:2021-12-31 15:18:38浏览:3089次
一:实现代码1
#!/bin/sh #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/Top" ]; then mkdir -p /Topfi #shell判断文件,目录是否存在或者具有权限 folder="/Top" file="/Top/test.txt" # -x 参数判断 $folder 是否存在并且是否具有可执行权限 if [ ! -x "$folder"]; then mkdir "$folder" fi # -d 参数判断 $folder 是否存在 if [ ! -d "$folder"]; then mkdir "$folder" fi # -f 参数判断 $file 是否存在 if [ ! -f "$file" ]; then touch "$file" fi # -n 判断一个"变量"是否有值 if [ ! -n "$file" ]; then echo "$file 变量为空!" exit 0 fi # 判断两个变量的字符串内容是否相同 if [ "$file1" = "$file2" ]; then echo "$file1 equal $file2" else echo "$file1 not equal $file2" fi
二.实现代码2
#!/bin/sh # 说明:判断文件是否存在 myPath="/Top" myFile="/Top/access.log" # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 if [ ! -x "$myPath"]; then mkdir "$myPath" fi # 这里的-d 参数判断$myPath是否存在 if [ ! -d "$myPath"]; then mkdir "$myPath" fi # 这里的-f参数判断$myFile是否存在 if [ ! -f "$myFile" ]; then touch "$myFile" fi # 其他参数还有-n,-n是判断一个变量是否是否有值 if [ ! -n "$myVar" ]; then echo "$myVar 变量为空!" exit 0 fi # 判断$file字符串内容是否是“123123”相同 if [ "$file1" = "123123" ]; then echo "$file1 equal $file2" else echo "$file1 not equal $file2" fi
栏目分类全部>
推荐文章
- 不写一行代码,完成一份最小可运行性的爬虫代码
- win10系统重装应用商店:安装路径不存在 该怎么办?
- windows批处理,批量替换文件、文件夹名中的空格
- mysql按条件统计count,及关联查询显示count为0的记录问题
- 解决composer install遇到:Your requirements could not be resolved to an installable set of packages
- Mysql用户权限配置
- GitHub加载缓慢无法访问 使用GitHubFast加快访问
- vue - element-ui -> el-tag 标签状态
- Linux相关知识点汇集
- TP6打印Sql语句
