如果条件语句,-x是什么意思?


Answers:


26

man bash页面(尤其是“条件表达式”部分):

   -a file
          True if file exists.
   -b file
          True if file exists and is a block special file.
   -c file
          True if file exists and is a character special file.
   -d file
          True if file exists and is a directory.
   -e file
          True if file exists.
   -f file
          True if file exists and is a regular file.
   -g file
          True if file exists and is set-group-id.
   -h file
          True if file exists and is a symbolic link.
   -k file
          True if file exists and its ``sticky'' bit is set.
   -p file
          True if file exists and is a named pipe (FIFO).
   -r file
          True if file exists and is readable.
   -s file
          True if file exists and has a size greater than zero.
   -t fd  True if file descriptor fd is open and refers to a terminal.
   -u file
          True if file exists and its set-user-id bit is set.
   -w file
          True if file exists and is writable.
   -x file
          True if file exists and is executable.

   [...]

3
应当注意,目录的可执行文件意味着可以遍历该目录。
rich remer

2
@StevenPenny问题的第二部分是“我如何才能找到此手册页?”
Sparhawk

1
@drewbenn test在bash中调用时,没有在调用test二进制文件。相反,您正在调用bash的test内置文件,该文件的文档位于help test和其他地方。man test因此,在某些情况下可能会产生误导。
克里斯·

11

if本身是一个shell关键字,因此您可以使用来找到有关它的信息help ifif本身仅根据下一条命令返回true(0)或false(非零)进行分支。不过,您真正想要的是man [man test,哪里[是的别名test。该语句实际上正在执行test -x /etc/rc.local,它将测试该文件是否存在并且是否可执行(或具有搜索权限)。


1
man [也可以。
Sparhawk

1
它不仅测试看是否存在,而且还测试文件是否可执行。
Tom Fenech

@TomFenech,啊,对...
psusi 2014年

@psusi if不是shell内置的shell关键字,运行此命令type if进行检查。
Avinash Raj 2014年

3

来自info test

`-x FILE'
    True if FILE exists and execute permission is granted (or search permission, if it is a directory).

必须具有目录的执行权限才能将其插入CD(也就是说,使某个目录成为当前的工作目录)。

在目录上需要执行以访问其中文件的“ inode”信息。您需要使用它来搜索目录以读取其中文件的索引节点。因此,通常将目录的执行许可权称为搜索许可权。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.