文件运算符

 文件运算符  描述
-b file  检测 file 是否为块设备文件
-c file  检测 file 是否为字符设备文件 
-d file  检测 file 是否为目录
-e file  检测 file 是否存在
-f file  检测 file 是否存在为普通文件
-r file  检测 file 是否可读
-s file  检测 file 是否为空文件
-w file  检测 file 是否可写
-x file  检测 file 是否可执行
-L file  检测 file 是否符号链接

实例

a) 检测 /dev/sr0 是否为块设备文件。

f="/dev/sr0"
if [ -b "$f" ]
then
echo "${f} is a block (buffered) special file."
else
echo "${f} is not a block (buffered) special file."
fi

b) 检测 /dev/null 是否为字符设备文件。

f="/dev/null"
if [ -c "$f" ]
then
echo "${file} is a character (unbuffered) special file."
else
echo "${file} is not a character (unbuffered) special file."
fi

c) 检测 /etc 是否为目录。

f="/etc"
if [ -d "$f" ]
then
echo "${file} is a directory."
else
echo "${file} is not a directory."
fi

d) 检测 /etc/passwd 是否为普通文件。

f="/etc/passwd"
if [ -f "$f" ]
then
echo "${file} is a regular file."
else
echo "${file} is not a regular file."
fi

e) 检测 /bin/mail 是否为符合链接。

f="/bin/mail"
if [ -L "$f" ]
then
echo "${file} is a symbolic link."
else
echo "${file} is not a symbolic link."
fi

f) 检测 /etc/passwd 是否可读。

f="/etc/passwd"
if [ -r "$f" ]
then
echo "${file} is readable."
else
echo "${file} is not readable."
fi

g) 检测 /etc/passwd 是否可写。

f="/etc/passwd"
if [ -w "$f" ]
then
echo "${file} is writable."
else
echo "${file} is not writable."
fi

h) 检测 /etc/passwd 是否可执行。

f="/etc/passwd"
if [ -x "$f" ]
then
echo "${file} is executable."
else
echo "${file} is not executable."
fi

i) 检测 /etc/passwd 是否存在。

f="/etc/passwd"
if [ -e "$f" ]
then
echo "${file} is existent."
else
echo "${file} is nonexistent."
fi

j) 检测 /etc/passwd 是否为空。

f="/etc/passwd"
if [ -s "$f" ]
then
echo "${file} is empty."
else
echo "${file} is not empty."
fi

Shell - 文件运算符的更多相关文章

  1. (转)Shell——基本运算符

    Shell 基本运算符 原文:http://blog.csdn.net/sinat_36053757/article/details/70319481 Shell 和其他编程语言一样,支持多种运算符, ...

  2. 五、Shell 基本运算符

    Shell 基本运算符 Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令 ...

  3. Shell文件权限和脚本执行

    一.预备知识 1.shell的作用   2.常识 (1)Tab键自动补全   使用Terminal时,输入命令的前几个字母,敲tab会自动补全命令或文件名.目录等. 好处:操作速度更快:不容易出错: ...

  4. Linux Shell系列教程之(十七) Shell文件包含

    本文是Linux Shell系列教程的第(十七)篇,更多Linux Shell教程请看:Linux Shell系列教程 通过文件包含,可以引用其他文件的内容,也可以将复杂内容分开,使程序结构更加清晰. ...

  5. 轻松学习Linux之Shell文件和目录属性详解

    轻松学习Linux之Shell文件和目录属性详解 轻松学习Linux之理解Sitcky 轻松学习Linux之理解umask 轻松学习Linux之理解SUID&SGUID 本系列多媒体教程已完成 ...

  6. Shell 文件包含

    和其他语言一样,Shell 也可以包含外部脚本.这样可以很方便的封装一些公用的代码作为一个独立的文件. Shell 文件包含的语法格式如下: . filename # 注意点号(.)和文件名中间有一空 ...

  7. Python第一天 安装 shell 文件

    Python第一天  安装  shell  文件 python里面一切都是对象 object 代码缩进:建议用四个空格来缩进,不要用tab键 安装 Linux自带python,windows需要下载m ...

  8. 用shell脚本新建shell文件并自动生成头说明信息

    目标: 新建文件后,直接给文件写入下图信息 代码实现: [root@localhost test]# vi AutoHead.sh #!/bin/bash#此程序的功能是新建shell文件并自动生成头 ...

  9. makefile 和shell文件相互调用

    shell 文件内调用makefile文件:   #!/bin/bash cd ctemplate-2.1./configuresudo make -f installcd ../cd Templat ...

随机推荐

  1. golang入门--一个简单的http client

    看完<Go Web 编程>的前两章就可以开始写代码了. import ( "fmt" "io/ioutil" "log" &qu ...

  2. matlab在图片上画框

    matlab在图片上画框 之前写过一个MATLAB在图片上画框的代码, http://blog.csdn.net/carson2005/article/details/17262811 最近使用后发现 ...

  3. QiQi and Symmerty

    http://sdu.acmclub.com/index.php?app=problem_title&id=961&problem_id=23772 题意:给出一个01串,问有多少个子 ...

  4. oracle索引再论

    ORACLE中索引的数据结构有B树结构和位图结构. 我们通常用的普通索引.反向键索引.函数索引等都是B树结构的,是树状结构:位图结构则只有叶子节点. B树索引操作有唯一性扫描,范围扫描,快速索引全扫描 ...

  5. Java多线程(四)之ConcurrentSkipListMap深入分析

    一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下,ConcurrentHashMap 存取速度是ConcurrentSki ...

  6. CSS3 :nth-of-type() 选择器

    可以设定第几个元素的样式 案例 css .qrcode img { margin-top: 30px; } .qrcode p:nth-of-type(1) { /*第一个p*/ font-size: ...

  7. BZOJ1603: [Usaco2008 Oct]打谷机

    1603: [Usaco2008 Oct]打谷机 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 602  Solved: 458[Submit][Stat ...

  8. showMonth

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 【转】linux(Ubuntu)配置svn仓库,搭建svn服务器

    原文网址:http://blog.1v2d.com/322.html 在家里搞了好久,终于搞出来,并且在线上已经成功搭建成功,在这感谢一个博主的文章,本篇文章也主要是转载他的内容,写的非常好,而且非常 ...

  10. 【转】Ubuntu下搭建SVN环境-Apache

    原文网址:http://www.cnblogs.com/candle806/archive/2012/12/20/2826280.html 环境描述:ubuntu server 12.04  / sv ...