test

评估一个表达式
如果条件为真,则返回一个 0 值。如果表达式不为真,则返回一个大于 0 的值

语法

  test expr
     [ expr ]
    [[ expr ]]

可以使用以下运算符组合多个表达式,按优先级的降序列出

   ! expr          如果expr为false则为True.
  ( expr )         返回的值EXPR。这可以用于覆盖运算符的正常优先级。

  expr1 && expr2   如果expr1  和 expr2都为真,则为真

  expr1 || expr2   如果expr1  或 expr2为真,则为真。

文件类型测试

 这些选项测试类型的文件,如果文件存在,所有情况都只返回True(0)

-b file       如果文件是 Block device,则为真. [[ -b demofile ]]

 -c file      如果文件是 Character device,则为真. [[ -c demofile ]]

 -d file      如果文件是目录,则为真. [[ -d demofile ]]

 -e file      如果文件存在,为真. [[ -e demofile ]]

 -f file      如果文件是常规文件,为真. [[ -f demofile ]]

 -g file      如果文件具有 set-group-id 属性,则为真. [[ -g demofile ]]

 -G file      如果文件属于当前有效组id,则为真. [[ -G demofile ]]

 -k file      如果文件具有 "sticky bit" 属性,则为真. [[ -k demofile ]]

 -h file      如果文件是符号链接,则为真. [[ -h demofile ]]
 -L file      如果文件是符号链接,则为真. [[ -L demofile ]]

 -O file      如果文件属于当前有效用户id,则为真. [[ -O demofile ]]

 -p file      如果文件是命名管道,则为真. [[ -p demofile ]]

 -r file      如果文件可读,则为真. [[ -r demofile ]]

 -S file      如果文件是Socket,则为真. [[ -S demofile ]]

 -s file      如果文件的大小大于零,则为真. [[ -s demofile ]]

 -t [FD]      如果FD在终端上打开,则为真。如果省略FD,默认值为1(标准输出). [[ -t demofile ]]

 -u file      如果文件具有 set-user-id 属性,则为真. [[ -L demofile ]]

 -w file      如果文件是可写的,则为真. [[ -w demofile ]]

 -x file      如果文件是可执行的,则为真. [[ -x demofile ]]

file1 -ef file2    如果file1和file2具有相同的设备和inode号,即它们是彼此的硬链接,则为真

例子



    test -1 -gt -2 && echo yes
    # yes
    test -l abc -gt 1 && echo yes
    # yes
    test 0x100 -eq 1
    # error--> test: integer expression expected before -eq
测试 paris.txt是可读的? 返回的值为false(非零)

    test -r paris.txt
    echo $?
    # 1
测试文件amsterdam.txt是否存在:

If [[ -e amsterdam.txt ]]; then   
  echo "we found the file"
fi
测试登录的用户名,如果 logname 变量= scott ,则返回TRUE(0)):
if [[ "$LOGNAME" = "scott" ]]; then
    echo "Logged in as Scott"
else
  echo "incorrect user"
fi

test 命令实例:

linux命令:test.bat parm1 parm2 parm3 parm3 parm2 parm1 test.bat test.bat parm1 parm2 parm3rootopen.com
linux命令:test.bat parm1 parm2 parm3 parm3 parm2 parm1 test.bat test.bat parm1 parm2 parm3rootopen.com
2018-06-08 22:21:48

linux命令:test `uname` = Linux && lsb_release -a || ( test `uname` = SunOS && cat /etc/release || uname -rms ) ) -rms uname || /etc/release cat && SunOS = `uname` test ( || -a lsb_release && Linux = `uname` test test `uname` = Linux && lsb_release -a || ( test `uname` = SunOS && cat /etc/release || uname -rms )rootopen.com
linux命令:test `uname` = Linux && lsb_release -a || ( test `uname` = SunOS && cat /etc/release || uname -rms ) ) -rms uname || /etc/release cat && SunOS = `uname` test ( || -a lsb_release && Linux = `uname` test test `uname` = Linux && lsb_release -a || ( test `uname` = SunOS && cat /etc/release || uname -rms )rootopen.com
2018-05-01 12:01:48

linux命令:teste.txt < cut -d : -f 1,5 /etc/passwd | tr : \\t | tr a-z A-Z | cat teste.txt teste.txt cat | A-Z a-z tr | \\t : tr | /etc/passwd 1,5 -f : -d cut < teste.txt teste.txt < cut -d : -f 1,5 /etc/passwd | tr : \\t | tr a-z A-Z | cat teste.txtrootopen.com
linux命令:teste.txt < cut -d : -f 1,5 /etc/passwd | tr : \\t | tr a-z A-Z | cat teste.txt teste.txt cat | A-Z a-z tr | \\t : tr | /etc/passwd 1,5 -f : -d cut < teste.txt teste.txt < cut -d : -f 1,5 /etc/passwd | tr : \\t | tr a-z A-Z | cat teste.txtrootopen.com
2018-04-28 19:47:48

linux命令:test -d folder || mkdir folder folder mkdir || folder -d test test -d folder || mkdir folderrootopen.com
linux命令:test -d folder || mkdir folder folder mkdir || folder -d test test -d folder || mkdir folderrootopen.com
2018-04-07 02:48:48

linux命令:test $((RANDOM%2)) -eq 0 0 -eq $((RANDOM%2)) test test $((RANDOM%2)) -eq 0rootopen.com
linux命令:test $((RANDOM%2)) -eq 0 0 -eq $((RANDOM%2)) test test $((RANDOM%2)) -eq 0rootopen.com
2018-02-09 03:21:48

linux命令:testt(){ o=abcdefghLkprsStuwxOGN;echo $@;for((i=0;i<${#o};i++));do c=${o:$i:1};test -$c $1 && help test | sed "/^ *-$c/!d;1q;s/^[^T]*/-$c /;s/ if/ -/";done; } } -/";done; if/ /;s/ *-$c/!d;1q;s/^[^T]*/-$c "/^ sed | test help && $1 -$c c=${o:$i:1};test $@;for((i=0;i<${#o};i++));do o=abcdefghLkprsStuwxOGN;echo testt(){ testt(){ o=abcdefghLkprsStuwxOGN;echo $@;for((i=0;i<${#o};i++));do c=${o:$i:1};test -$c $1 && help test | sed "/^ *-$c/!d;1q;s/^[^T]*/-$c /;s/ if/ -/";done; }rootopen.com
linux命令:testt(){ o=abcdefghLkprsStuwxOGN;echo $@;for((i=0;i<${#o};i++));do c=${o:$i:1};test -$c $1 && help test | sed "/^ *-$c/!d;1q;s/^[^T]*/-$c /;s/ if/ -/";done; } } -/";done; if/ /;s/ *-$c/!d;1q;s/^[^T]*/-$c "/^ sed | test help && $1 -$c c=${o:$i:1};test $@;for((i=0;i<${#o};i++));do o=abcdefghLkprsStuwxOGN;echo testt(){ testt(){ o=abcdefghLkprsStuwxOGN;echo $@;for((i=0;i<${#o};i++));do c=${o:$i:1};test -$c $1 && help test | sed "/^ *-$c/!d;1q;s/^[^T]*/-$c /;s/ if/ -/";done; }rootopen.com
2018-04-24 20:17:48

linux命令:test "$(md5sum /local/file | cut -d' ' -f1)" == "$(ssh root@xen -- md5sum /remote/file | cut -d' ' -f1)" && echo "Match" || echo "Differ" "Differ" echo || "Match" echo && -f1)" ' -d' cut | /remote/file md5sum -- root@xen "$(ssh == -f1)" ' -d' cut | /local/file "$(md5sum test test "$(md5sum /local/file | cut -d' ' -f1)" == "$(ssh root@xen -- md5sum /remote/file | cut -d' ' -f1)" && echo "Match" || echo "Differ"rootopen.com
linux命令:test "$(md5sum /local/file | cut -d' ' -f1)" == "$(ssh root@xen -- md5sum /remote/file | cut -d' ' -f1)" && echo "Match" || echo "Differ" "Differ" echo || "Match" echo && -f1)" ' -d' cut | /remote/file md5sum -- root@xen "$(ssh == -f1)" ' -d' cut | /local/file "$(md5sum test test "$(md5sum /local/file | cut -d' ' -f1)" == "$(ssh root@xen -- md5sum /remote/file | cut -d' ' -f1)" && echo "Match" || echo "Differ"rootopen.com
2018-02-11 13:27:48

linux命令:test `uname` = Linux && lsb_release -a 2>/dev/null || ( test `uname` = SunOS && cat /etc/release || uname -rms ) ) -rms uname || /etc/release cat && SunOS = `uname` test ( || 2>/dev/null -a lsb_release && Linux = `uname` test test `uname` = Linux && lsb_release -a 2>/dev/null || ( test `uname` = SunOS && cat /etc/release || uname -rms )rootopen.com
linux命令:test `uname` = Linux && lsb_release -a 2>/dev/null || ( test `uname` = SunOS && cat /etc/release || uname -rms ) ) -rms uname || /etc/release cat && SunOS = `uname` test ( || 2>/dev/null -a lsb_release && Linux = `uname` test test `uname` = Linux && lsb_release -a 2>/dev/null || ( test `uname` = SunOS && cat /etc/release || uname -rms )rootopen.com
2018-06-10 17:29:48

Linux的Bash命令行(A-Z排序)