15. 条件测试的概念

路径测试

[root@localhost test]# test -d /home
[root@localhost test]# echo $?
0
[root@localhost test]# test -d /hoem
[root@localhost test]# echo $?
1
[root@localhost test]# cat mysql_backup_test
#!/usr/bin/bash
back_dir=/var/mysql_back
if ! test -d $back_dir; then
        mkdir -p $back_dir
fi

echo "MySQL Backup Ongoing..."

[root@localhost test]# chmod a+x mysql_backup_test

[root@localhost test]# ./mysql_backup_test
MySQL Backup Ongoing...

debug执行

[root@localhost test]# bash -vx mysql_backup_test
#!/usr/bin/bash
back_dir=/var/mysql_back
+ back_dir=/var/mysql_back
if ! test -d $back_dir; then
        mkdir -p $back_dir
fi
+ test -d /var/mysql_back

echo "MySQL Backup Ongoing..."
+ echo 'MySQL Backup Ongoing...'
MySQL Backup Ongoing...
[root@localhost test]#

如果没有root权限,就给出提示,并退出。

[root@localhost test]# cat httpd_install.sh
#!/usr/bin/bash
if [ $UID -ne 0 ];then
        echo "You are not root,root required"
        exit
fi
yum -y install httpd

[root@localhost test]# chmod a+x httpd_install.sh

[root@localhost test]# ./httpd_install.sh
Loaded plugins: fastestmirror
[root@localhost test]# ./httpd_install.sh
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Could not get metalink https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=x86_64 error was
12: Timeout on https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=x86_64: (28, 'Operation timed out after 30004 milliseconds with 0 out of 0 bytes received')
 * base: mirrors.ustc.edu.cn
 * epel: mirror.poliwangi.ac.id
 * extras: mirrors.cn99.com
 * updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================================================
 Package                                                           Arch                                                         Version                                                                     Repository                                                  Size
=============================================================================================================================================================================================================================================================================
Installing:
 httpd                                                             x86_64                                                       2.4.6-90.el7.centos                                                         base                                                       2.7 M
Installing for dependencies:
 httpd-tools                                                       x86_64                                                       2.4.6-90.el7.centos                                                         base                                                        91 k
 mailcap                                                           noarch                                                       2.1.41-2.el7                                                                base                                                        31 k

Transaction Summary
=============================================================================================================================================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 2.8 M
Installed size: 9.6 M
Downloading packages:
(1/3): mailcap-2.1.41-2.el7.noarch.rpm                                                                                                                                                                                                                |  31 kB  00:00:01
(2/3): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm                                                                                                                                                                                                     |  91 kB  00:00:01
(3/3): httpd-2.4.6-90.el7.centos.x86_64.rpm                                                                                                                                                                                                           | 2.7 MB  00:00:07
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                        388 kB/s | 2.8 MB  00:00:07
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                                                                                                                                                    1/3
  Installing : mailcap-2.1.41-2.el7.noarch                                                                                                                                                                                                                               2/3
  Installing : httpd-2.4.6-90.el7.centos.x86_64                                                                                                                                                                                                                          3/3
  Verifying  : mailcap-2.1.41-2.el7.noarch                                                                                                                                                                                                                               1/3
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                                                                                                                                                    2/3
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                                                                                                                                                                                                                          3/3

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos

Dependency Installed:
  httpd-tools.x86_64 0:2.4.6-90.el7.centos                                                                                                   mailcap.noarch 0:2.1.41-2.el7

Complete!

测试条件

[root@localhost test]# man test

TEST(1)                                                                                                                      User Commands                                                                                                                     TEST(1)

NAME
       test - check file types and compare values

SYNOPSIS
       test EXPRESSION
       test

       [ EXPRESSION ]
       [ ]
       [ OPTION

DESCRIPTION
       Exit with the status determined by EXPRESSION.

       --help display this help and exit

       --version
              output version information and exit

       An omitted EXPRESSION defaults to false.  Otherwise, EXPRESSION is true or false and sets exit status.  It is one of:

       ( EXPRESSION )
              EXPRESSION is true

       ! EXPRESSION
              EXPRESSION is false

       EXPRESSION1 -a EXPRESSION2
              both EXPRESSION1 and EXPRESSION2 are true

       EXPRESSION1 -o EXPRESSION2
              either EXPRESSION1 or EXPRESSION2 is true

       -n STRING
              the length of STRING is nonzero

       STRING equivalent to -n STRING

       -z STRING
              the length of STRING is zero

       STRING1 = STRING2
              the strings are equal

       STRING1 != STRING2
              the strings are not equal

       INTEGER1 -eq INTEGER2
              INTEGER1 is equal to INTEGER2

       INTEGER1 -ge INTEGER2
              INTEGER1 is greater than or equal to INTEGER2

       INTEGER1 -gt INTEGER2
              INTEGER1 is greater than INTEGER2

       INTEGER1 -le INTEGER2
              INTEGER1 is less than or equal to INTEGER2

       INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2

       INTEGER1 -ne INTEGER2
              INTEGER1 is not equal to INTEGER2

       FILE1 -ef FILE2
              FILE1 and FILE2 have the same device and inode numbers

       FILE1 -nt FILE2
              FILE1 is newer (modification date) than FILE2

       FILE1 -ot FILE2
              FILE1 is older than FILE2

       -b FILE
              FILE exists and is block special

       -c FILE
              FILE exists and is character special

       -d FILE
              FILE exists and is a directory

       -e FILE
              FILE exists

       -f FILE
              FILE exists and is a regular file

       -g FILE
              FILE exists and is set-group-ID

       -G FILE
              FILE exists and is owned by the effective group ID

       -h FILE
              FILE exists and is a symbolic link (same as -L)

       -k FILE
              FILE exists and has its sticky bit set

       -L FILE
              FILE exists and is a symbolic link (same as -h)

       -O FILE
              FILE exists and is owned by the effective user ID

       -p FILE
              FILE exists and is a named pipe

       -r FILE
              FILE exists and read permission is granted

       -s FILE
              FILE exists and has a size greater than zero

       -S FILE
              FILE exists and is a socket

       -t FD  file descriptor FD is opened on a terminal

       -u FILE
              FILE exists and its set-user-ID bit is set

       -w FILE
              FILE exists and write permission is granted

       -x FILE
              FILE exists and execute (or search) permission is granted

       Except for -h and -L, all FILE-related tests dereference symbolic links.  Beware that parentheses need to be escaped (e.g., by backslashes) for shells.  INTEGER may also be -l STRING, which evaluates to the length of STRING.

       NOTE: [ honors the --help and --version options, but test does not.  test treats each of those as it treats any other nonempty STRING.

       NOTE: your shell may have its own version of test and/or [, which usually supersedes the version described here.  Please refer to your shell's documentation for details about the options it supports.

       GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report test translation bugs to <http://translationproject.org/team/>

AUTHOR
       Written by Kevin Braunsdorf and Matthew Bradburn.

COPYRIGHT
       Copyright ? 2013 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       The full documentation for test is maintained as a Texinfo manual.  If the info and test programs are properly installed at your site, the command

              info coreutils 'test invocation'
发布了423 篇原创文章 · 获赞 134 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/f2157120/article/details/105695423