• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > 判断文件是否存在的shell脚本代码

判断文件是否存在的shell脚本代码

作者:mdxy-dxy 字体:[增加 减小] 来源:互联网

mdxy-dxy 通过本文主要向大家介绍了shell脚本读取文件,shell脚本创建文件,shell脚本删除文件,shell脚本文件,shell脚本写文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

实现代码一、

#!/bin/sh
# 判断文件是否存在
# link:www.jb51.net
# date:2013/2/28

myPath="/var/log/httpd/"
myFile="/var /log/httpd/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 is empty"
 exit 0
fi

# 两个变量判断是否相等
if [ "$var1" = "$var2" ]; then
 echo '$var1 eq $var2'
else
 echo '$var1 not eq $var2'
fi
</div>

实现代码二、

#shell判断文件夹是否存在

#如果文件夹不存在,创建文件夹
if [ ! -d "/myfolder" ]; then
 mkdir /myfolder
fi

#shell判断文件,目录是否存在或者具有权限

folder="/var/www/"
file="/var/www/log"

# -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 "$var" ]; then
 echo "$var is empty"
 exit 0
fi

# 判断两个变量是否相等
if [ "$var1" = "$var2" ]; then
 echo '$var1 eq $var2'
else
 echo '$var1 not eq $var2'
fi
</div>

-f 和-e的区别

Conditional Logic on Files

-a file exists.

-b file exists and is a block special file.

-c file exists and is a character special file.

-d file exists and is a directory.

-e file exists (just the same as -a).

-f file exists and is a regular file.

-g file exists and has its setgid(2) bit set.

-G file exists and has the same group ID as this process.

-k file exists and has its sticky bit set.

-L file exists and is a symbolic link.

-n string length is not zero.

-o Named option is set on.

-O file exists and is owned by the user ID of this process.

-p file exists and is a first in, first out (FIFO) special file or

named pipe.

-r file exists and is readable by the current process.

-s file exists and has a size greater than zero.

-S file exists and is a socket.

-t file descriptor number fildes is open and associated with a

terminal device.

-u file exists and has its setuid(2) bit set.

-w file exists and is writable by the current process.

-x file exists and is executable by the current process.

-z string length is zero.

是用 -s 还是用 -f 这个区别是很大的!

</div>

您可能想查找下面的文章:

  • Shell脚本切割tomcat的日志文件
  • Shell脚本实现根据文件的修改时间来分类文件
  • shell脚本实现实时检测文件变更
  • Shell脚本逐行读取文本文件(不改变文本格式)
  • Shell脚本一次读取文件中一行的2种写法
  • Shell脚本创建指定大小文件的测试数据
  • Shell脚本统计文件行数的8种方法
  • Shell脚本实现批量替换文件内容
  • Shell脚本实现删除一年前文件功能分享
  • Shell脚本实现删除一年前文件功能分享

相关文章

  • 一句话Shell命令关闭不需要的随机启动服务
  • 完美解决gvim的菜单乱码问题
  • SED单行脚本快速参考中文版(Unix 流编辑器)
  • Bash中数组的操作教程
  • Shell时间(date)相关命令
  • 简单的Linux查找后门思路和shell脚本分享
  • 深入理解Shell输出颜色与控制
  • 用shell+sendmail实现服务器监控报警小脚本
  • Shell脚本批量清除Nginx缓存
  • Cygwin下安装vim后,vim中退格键无法正常使用的解决方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • linux中mysql备份shell脚本代码
    • Shell根据日期、月份、星期判断年份的代码
    • 一天一个shell命令 linux文本操作系列-chmod命令用法
    • Shell脚本实现判断IP地址是否在一个ip段内代码分享
    • Linux Shell脚本系列教程(六):数组和关联数组
    • shell判断文件,目录是否存在或者具有权限的代码
    • bash scp command not found的解决方法
    • Shell脚本中通过正则表达式匹配IP地址
    • Shell脚本实现检测文件是否被修改过代码分享
    • Shell脚本实现的基于SVN的代码提交量统计工具

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有