Shell 判断文件是否存在的方法
方法一:
locate_library() { [ ! "$1*" = "`echo $1*`" ]; }
用法:
# Look for PAM (done weirdly due to distribution bugs (e.g. Debian) or the
# crypt library.
if find_func pam_start sysdeputil.o; thenlocate_library /lib/libpam.so.0 && echo "/lib/libpam.so.0";locate_library /usr/lib/libpam.so && echo "-lpam";locate_library /usr/lib64/libpam.so && echo "-lpam";# HP-UX ends shared libraries with .sllocate_library /usr/lib/libpam.sl && echo "-lpam";# AIX ends shared libraries with .alocate_library /usr/lib/libpam.a && echo "-lpam";
elselocate_library /lib/libcrypt.so && echo "-lcrypt";locate_library /usr/lib/libcrypt.so && echo "-lcrypt";locate_library /usr/lib64/libcrypt.so && echo "-lcrypt";
fi
方法二:
#!/bin/sh
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
各种判断标识及含义如下:
-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.
Shell 判断文件是否存在的方法相关推荐
- shell判断文件是否存在[转]
原文出处: http://canofy.iteye.com/blog/252289 shell判断文件,目录是否存在或者具有权限#!/bin/shmyPath="/var/log/httpd ...
- python语句结束符_python中判断文件结束符的具体方法
python中判断文件结束符的方法:可以使用try except语句块来进行判断. 具体使用方法:[try:while True:s = input() except EOFError: pass]. ...
- c 判断文件是否存在_C++基础:判断文件是否存在的方法
C++基础:判断文件是否存在的方法 使用以下几种函数判断文件是否存在 #include <fstream> --> 使用ifstream打开文件流,成功则存在,失败则不存在; #in ...
- shell——判断文件是否存在
初次使用Markdown编辑器来编写博客,希望有不一样的体会 本文通过网络文章来体会下shell中对文件的操作 1.这个编辑器和wiki有点像呢 2.Shell是一个很好用的脚本 以下为shell下的 ...
- java判断方法是否存在_java判断文件是否存在的方法
java判断文件是否存在的方法 发布时间:2020-06-24 13:09:50 来源:亿速云 阅读:140 作者:Leah 本篇文章展示了java判断文件是否存在的具体操作,代码简明扼要容易理解,绝 ...
- python中判断文件是否存在的三种方式_Python中判断文件是否存在的方法
Python中判断文件是否存在的方法 发布时间:2020-08-06 11:15:42 来源:亿速云 阅读:58 作者:小新 这篇文章主要介绍Python中判断文件是否存在的方法,文中介绍的非常详细, ...
- python 判断文件的几种方法
判断文件的几种方法及特点对比 我们知道当文件不存在的时候,open () 方法的写模式与追加模式都会新建文件,但是对文件进行判断的场景还有很多,比如,在爬虫下载的时候,可能需要判断文件是否存在,以免重 ...
- python怎么判断一个文件是否存在-python判断文件是否存在的方法
python如何判断文件是否存在 通常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错.所以最好在做任何操作之前,先判断文件是否存在. python中如何判断文件是否存在? ...
- shell判断文件,目录是否存在或者具有权限 (转载)
转自:http://cqfish.blog.51cto.com/622299/187188 文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d6 ...
最新文章
- vmware克隆虚拟机
- 微信小程序开发--如何在swiper中显示两个item以及下一个item的部分内容
- 请不要做浮躁的人——转给即将上路或者正在路上的程序员朋友
- 20220207-CTF-MISC-第11题--- base64隐写--附带脚本
- 《MySQL——主备切换流程与主备延迟》
- 破洞牛仔裤中的几何学:简单理解万有覆叠问题
- css演练 0920 个人简历的制作
- Redisbook学习笔记(3)数据类型之列表
- 计算机图形学——Bresenham画线算法
- linux系统 32位 64位 jdk下载 jdk6 jdk7
- SIM900A 发送中英文短信
- 1.2 逻辑电路基础
- 手机分辨率基础知识(DPI,DIP计算)
- Thrift 小试牛刀
- (1)mysql--查询成绩最高和最低的人
- u盘软件怎么安装linux,教你怎么使用u盘安装Linux平台
- ACM-ICPC 2018 南京赛区网络预赛丨AC Challenge丨状压DP
- 知识图谱课程报告-关系抽取文献综述
- 融云直播 SDK 玩法翻新,入围信通院「实时互动创新应用优秀案例」
- CLINT和PLIC,risc-v中断