IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

shell文件存在相关判断参数

Zhang Jiuan' Notes 2010-01-26 16:52:50 累计浏览 4,840 次
本机暂存

The tests below are test conditions provided by the shell:

    * -b file = True if the file exists and is block special file.
    * -c file = True if the file exists and is character special file.
    * -d file = True if the file exists and is a directory.
    * -e file = True if the file exists.
    * -f file = True if the file exists and is a regular file
    * -g file = True if the file exists and the set-group-id bit is set.
    * -k file = True if the files’ “sticky” bit is set.
    * -L file = True if the file exists and is a symbolic link.
    * -p file = True if the file exists and is a named pipe.
    * -r file = True if the file exists and is readable.
    * -s file = True if the file exists and its size is greater than zero.
    * -s file = True if the file exists and is a socket.
    * -t fd = True if the file descriptor is opened on a terminal.
    * -u file = True if the file exists and its set-user-id bit is set.
    * -w file = True if the file exists and is writable.
    * -x file = True if the file exists and is executable.
    * -O file = True if the file exists and is owned by the effective user id.
    * -G file = True if the file exists and is owned by the effective group id.
    * file1 -nt file2 = True if file1 is newer, by modification date, than file2.
    * file1 ot file2 = True if file1 is older than file2.
    * file1 ef file2 = True if file1 and file2 have the same device and inode numbers.
    * -z string = True if the length of the string is 0.
    * -n string = True if the length of the string is non-zero.
    * string1 = string2 = True if the strings are equal.
    * string1 != string2 = True if the strings are not equal.
    * !expr = True if the expr evaluates to false.
    * expr1 -a expr2 = True if both expr1 and expr2 are true.
    * expr1 -o expr2 = True is either expr1 or expr2 is true.

Shell中的数据问题

   ArrayName=(”element 1″ “element 2″ “element 3″)  #数组定义
   echo ${#ArrayName[@]}
  echo “The number of elements in the array is ${#ArrayName[*]}”    
   
    例子
    #!/bin/bash
    # define array
    # name server names FQDN
    NAMESERVERS=(”ns1.nixcraft.net.” “ns2.nixcraft.net.” “ns3.nixcraft.net.”)
 
    # get length of an array
     tLen=${#NAMESERVERS[@]}
 
    # use for loop read all nameservers
    for (( i=0; i<${tLen}; i++ ));
    do
      echo ${NAMESERVERS[$i]}
    done

   Sample output:

     ns1.nixcraft.net.
     ns2.nixcraft.net.
     ns3.nixcraft.net.

同分类推荐文章

  1. 从零重建 macOS 开发机:可复现的环境初始化流程 (2026-06-14 20:36:00)
  2. 百度物理网络监控工具开源第二弹:毫秒级监控工具 baize,让你的网络问题无处遁形 (2026-06-11 08:10:28)
  3. How to Set Up Homebrew Tap for Private CLI Tools: A Complete Guide (2026-05-27 02:13:03)

查看更多 DevOps 文章 →

建议继续学习

  1. Bash的模式和配置文件加载 (累计阅读 24,406)
  2. 28个Unix/Linux的命令行神器 (累计阅读 16,789)
  3. 我常用的主机监控shell脚本 (累计阅读 13,429)
  4. 100个常用的linux命令 (累计阅读 11,605)
  5. Linux命令行里的“瑞士军刀” (累计阅读 11,584)
  6. 每个程序员都应该知道的8个Linux命令 (累计阅读 10,744)
  7. perl更新/修改/删除文本文件内容 (累计阅读 10,647)
  8. 最受欢迎的10个 Linux 单行命令 (累计阅读 10,129)
  9. linux下搜索find命令详解 (累计阅读 9,457)
  10. tomcat catalina.out日志切割每天生成一个文件 (累计阅读 9,240)