zoukankan      html  css  js  c++  java
  • shell条件测试test

    shell条件测试可以通过以下两种方式:

    test   参数    测试内容

    [ 参数  测试内容 ]

    一、测试文件类型:

    test  -e   文件名          (测试文件是否存在)

    [ -e  文件名 ]                测试文件是否存在,注意中括号两边的空格

    test -f   文件名             判断是否为普通文件

    test -d   文件名            判断是否为目录

    test -b   文件名            判断是否为块设备文件

    test -c   文件名            判断是否为字符设备文件

    二、测试文件权限:

    test -r  文件名        判断文件是否又读权限

    test -w  文件名       判断文件是否又写权限

    test -x  文件名        判断文件是否有执行权限

    test -s  文件名        判断文件是否为非空白,有内容为真

    三、两个文件比较:

    [ file1 -nt file2 ]     file1是否比file2更新

    [ file1 -ot file2 ]     file1是否比file2旧

    [ file1 -et file2 ]     fie1与file2是否是链接文件

    四、两个数组之间判断:

    [ n1 eq n2 ]       n1和n2是否相等

    [ n1 ne n2 ]       n1和n2是否不等

    [ n1 gt n2 ]       n1是否大于n2

    [ n1 lt n2 ]        n1是否小于n2

    [ n1 ge n2 ]       n1大于等于n2

    [ n1 le n2 ]        n1小于等于n2

    五、字符串判断:

    [ -z  字符串 ]     判断字符串是否为空

    [ 字符串1 == 字符串2]   判断字符串1是否等于字符串2

    [ 字符串1 != 字符串2]   判断字符串1是否不等于字符串2

    六、一个简单案例:

    #!/bin/bash
    read -p "please input a dir:"  dir
    if [ ! -e $dir ]
    then
            echo "filename is not exist"
            exit 1
    fi
    if [ ! -d $dir ]
    then
            echo "please input a dir"
            exit 1
    fi
    if [ -e $dir ]
    then
            for i in $(ls $dir)
                    do
                            echo $i
                    done
    fi
  • 相关阅读:
    final
    Leetcode Single Number
    Leetcode Implement strStr()
    Leetcode Count and Say
    Leetcode Paint Fence
    Leetcode Isomorphic Strings
    Leetcode Min Stack
    Leetcode Valid Sudoku
    Leetcode Two Sum III
    Leetcode Read N Characters Given Read4
  • 原文地址:https://www.cnblogs.com/liyuanhong/p/5698953.html
Copyright © 2011-2022 走看看