zoukankan      html  css  js  c++  java
  • linux文件测试语句

    1、Linux系统中文件测试语句用于判断文件是否存在、是文件或者目录、或者权限判断

       linux系统中 echo $? 用于判断上一句是否成功执行,成功返回0,不成功返回其他数字,

       简单示例: 

    [root@linuxprobe test]# ls
    [root@linuxprobe test]# mkdir test  ## 成功执行
    [root@linuxprobe test]# echo $?  ## 返回0
    0
    [root@linuxprobe test]# mkdir test  ##未成功执行
    mkdir: cannot create directory ‘test’: File exists
    [root@linuxprobe test]# echo $?  ## 返回1
    1

    2、格式 

       [ 条件表达式 ] ,条件表达式的两边一定要有空格

    3、简单测试  -e判断文件或者目录是否存在 

    [root@linuxprobe test]# ls
    a.txt  test
    [root@linuxprobe test]# [ -e a.txt ]
    [root@linuxprobe test]# echo $?  ## 存在返回0
    0
    [root@linuxprobe test]# [ -e b.txt ]  ## 不存在返回1
    [root@linuxprobe test]# echo $?
    1
    [root@linuxprobe test]# [ -e test ]  ##同上
    [root@linuxprobe test]# echo $?
    0
    [root@linuxprobe test]# [ -e test2 ]
    [root@linuxprobe test]# echo $?
    1

    4、-f判断是否为普通文件, -d 判断是否为目录

    [root@linuxprobe test]# touch a.txt  ## 普通文件
    [root@linuxprobe test]# mkdir test  ##目录
    [root@linuxprobe test]# ls -l
    total 0
    -rw-r--r--. 1 root root 0 Oct 14 06:53 a.txt
    drwxr-xr-x. 2 root root 6 Oct 14 06:53 test
    [root@linuxprobe test]# [ -f a.txt ] 
    [root@linuxprobe test]# echo $?  ## 普通文件返回0
    0
    [root@linuxprobe test]# [ -f test ]
    [root@linuxprobe test]# echo $?  ##目录,返回1
    1
    [root@linuxprobe test]# [ -d a.txt ] ## 普通文件,返回1
    [root@linuxprobe test]# echo $?
    1
    [root@linuxprobe test]# [ -d test ] ##目录,返回0
    [root@linuxprobe test]# echo $?
    0

    5、-r 、-w、-x分别进行权限判断

    [root@linuxprobe test]# ls
    [root@linuxprobe test]# touch a.txt b.txt
    [root@linuxprobe test]# chmod 000 a.txt
    [root@linuxprobe test]# chmod 777 b.txt
    [root@linuxprobe test]# ll -h  ## 查看权限
    total 0
    ----------. 1 root root 0 Oct 14 18:27 a.txt
    -rwxrwxrwx. 1 root root 0 Oct 14 18:27 b.txt
    [root@linuxprobe test]# whoami  ##root用户
    root
    [root@linuxprobe test]# [ -r a.txt ]   ## 对root无效?
    [root@linuxprobe test]# echo $?
    0
    [root@linuxprobe test]# [ -w a.txt ]   ## 对root无效?
    [root@linuxprobe test]# echo $?
    0
    [root@linuxprobe test]# [ -x a.txt ]  ## 执行权限可以
    [root@linuxprobe test]# echo $?
    1
    [root@linuxprobe test]# su - linuxprobe  ## 切换至普通用户
    [linuxprobe@linuxprobe ~]$ cd ../test/
    [linuxprobe@linuxprobe test]$ ll -h
    total 0
    ----------. 1 root root 0 Oct 14 18:27 a.txt
    -rwxrwxrwx. 1 root root 0 Oct 14 18:27 b.txt
    [linuxprobe@linuxprobe test]$ whoami
    linuxprobe
    [linuxprobe@linuxprobe test]$ [ -r a.txt ]  ## 对普通用户没有读的权限
    [linuxprobe@linuxprobe test]$ echo $?
    1
    [linuxprobe@linuxprobe test]$ [ -w a.txt ]  ## 同上
    [linuxprobe@linuxprobe test]$ echo $?
    1
    [linuxprobe@linuxprobe test]$ [ -x a.txt ]  ## 同上
    [linuxprobe@linuxprobe test]$ echo $?
    1
    [linuxprobe@linuxprobe test]$ ls
    a.txt  b.txt
    [linuxprobe@linuxprobe test]$ whoami
    linuxprobe
    [linuxprobe@linuxprobe test]$ ll -h
    total 0
    ----------. 1 root root 0 Oct 14 18:27 a.txt
    -rwxrwxrwx. 1 root root 0 Oct 14 18:27 b.txt
    [linuxprobe@linuxprobe test]$ [ -r b.txt ]
    [linuxprobe@linuxprobe test]$ echo $?
    0
    [linuxprobe@linuxprobe test]$ [ -w b.txt ]
    [linuxprobe@linuxprobe test]$ echo $?
    0
    [linuxprobe@linuxprobe test]$ [ -x b.txt ]
    [linuxprobe@linuxprobe test]$ echo $?
    0
    [linuxprobe@linuxprobe test]$ su - root
    Password:
    [root@linuxprobe ~]# cd /home/test/
    [root@linuxprobe test]# ll -h
    total 0
    ----------. 1 root root 0 Oct 14 18:27 a.txt
    -rwxrwxrwx. 1 root root 0 Oct 14 18:27 b.txt
    [root@linuxprobe test]# whoami
    root
    [root@linuxprobe test]# [ -r b.txt ]
    [root@linuxprobe test]# echo $?
    0
    [root@linuxprobe test]# [ -w b.txt ]
    [root@linuxprobe test]# echo $?
    0
    [root@linuxprobe test]# [ -x b.txt ]
    [root@linuxprobe test]# echo $?
    0

      

  • 相关阅读:
    ARCDesktop 学习笔记(一) 添加地图图层
    ARCGIS 开发常用技巧参考
    关于引用动软代码 找不到dll命名空间问题
    银行转账手续费
    Silverlight 笔记
    地图API
    (转)使用ImageBrush替换PictureMarkerSymbol以加强graphic显示性能
    WPF 网摘
    ArcCatalog添加GISserverIP问题
    ARCGIS 10 中文版 3D Analyst Tools等未授权问题
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13812212.html
Copyright © 2011-2022 走看看