类型(Bash 内置函数)的用途是什么?

Aks*_*til 19 bash shell-script

我在 Bash 脚本中遇到了一个命令,其中发现:

find /var/log/abcd -type f
Run Code Online (Sandbox Code Playgroud)

上面的命令是在清理日志文件的上下文中。我知道有什么find作用。

看过之后-type f,我查看了它的手册页。我必须在BASH_BUILTINS(1) 的手册页中看到它

type命令下 -f 标志的描述是:-

The -f option suppresses shell function lookup, as with the command builtin.
Run Code Online (Sandbox Code Playgroud)

以下是我的问题:

  1. 有什么用type
  2. -f国旗的意义是什么?
  3. using typewithfind命令有什么用?

[编辑]:-在阅读了到目前为止的所有评论和答案之后,我想提一下我对-type option in command findVs 的误解的原因type command。这一切都发生了,因为我一直假设直到日期只看到find带有单个减号 '-'的短选项(在命令的情况下测试),例如,ls -l. 大多数情况下,我看到带有双减号 '--' 的长选项,例如ls --version.

Run*_*ium 24

在这种情况下type与内置的 bash 无关type,但稍后会详细介绍。

关于“类型”的一点

BASH 内置type命令为您提供有关命令的信息。因此:

$ type type
type is a shell builtin
Run Code Online (Sandbox Code Playgroud)

语法是:

type [-tap] [name ...]
Run Code Online (Sandbox Code Playgroud)
  • -t: 只打印类型,如果找到
  • -a: 打印所有出现的命令,包括内置命令和其他命令。
  • -p: 打印将在调用命令时执行的磁盘文件,或者什么也不打印。

如果我们看一下timekillcat作为一个例子:

$ type time kill cat
time is a shell keyword
kill is a shell builtin
cat is /bin/cat

$ type -t time kill cat
keyword
builtin
file

$ type -a time kill cat
time is a shell keyword
time is /usr/bin/time
kill is a shell builtin
kill is /bin/kill
cat is /bin/cat

$ type -ta time kill cat
keyword
file
builtin
file
file
Run Code Online (Sandbox Code Playgroud)

现在,这指定如果您在 Bash shell 中并键入time some_cmdtime则使用bash 内置命令。要使用该系统,time您可以这样做/usr/bin/time some_cmd

通常用于确保使用系统而非内置命令的一种方法是使用which.

tt=$(which time)
Run Code Online (Sandbox Code Playgroud)

然后用于$tt调用 system time


有问题的命令

在这种情况下,-type是命令的一个选项find。该选项采用一个参数来指定实体的类型。例子

find . -type f  # File
find . -type d  # Directory
Run Code Online (Sandbox Code Playgroud)

还有更多,检查man find其余的。

要搜索您可以执行的特定选项(在 man 中):

/^\s*-类型Enter

然后使用nfor next直到找到为止。


关于shell命令的一点

这是个人的一点解读。

在这种特定情况下,一些值得一提的事情是命令、选项、参数和管道。

这有点松散地使用,但在我的词汇中,我们简而言之:

  • 命令:程序内置.
  • 参数:命令字后的实体。
  • option:可选参数。
  • 参数:必需的参数。

在命令规范中,方括号用于指定选项,然后可选地使用更少/更大来指定参数。因此:

foo [-abs] [-t <bar>] <file> ...
foo [-abs] [-t bar] file ...
Run Code Online (Sandbox Code Playgroud)

给出-a -b-s作为可选参数,并且file是必需的。 -t是可选的,但如果指定则采用必需的参数bar。点表示它可以包含多个文件。

这不是确切的规范,并且经常manhelp需要确定。

参数选项和输入的定位通常会混淆,但通常最好保持基于位置的方法,因为某些系统不处理参数的混合定位。举个例子:

chmod -R nick 722 foo
chmod nick 722 foo -R
Run Code Online (Sandbox Code Playgroud)

两者都适用于某些系统,而后者不适用于其他系统。


在您的确切命令中,所有参数都属于find- 因此,如果您想知道某个属性man find是正确的查看位置。如果您需要查看 shell 等的手册页,可以在例如:

find . $(some command)
find . `some command`
find . $some_var
find . -type f -exec some_command {} \;
find . -type f | some_command
...
Run Code Online (Sandbox Code Playgroud)

The-exec是一个特殊的,其中-exec some_command {} \;所有参数都给find,但some_command {} \;部分在findto内展开some_command string_of_found_entity


进一步

  • 引用
  • 扩张
  • 命令替换
  • 还有更多

您可能会发现这很有用


rus*_*ush 8

你建议立即进行删除在看man find,而不是help typeman bashtypeinfind将指定您需要的文件类型:

   -type c
          File is of type c:

          b      block (buffered) special

          c      character (unbuffered) special

          d      directory

          p      named pipe (FIFO)

          f      regular file

          l      symbolic link; this is never true if the -L option or the
                 -follow option is in effect, unless the symbolic link  is
                 broken.  If you want to search for symbolic links when -L
                 is in effect, use -xtype.

          s      socket

          D      door (Solaris)
Run Code Online (Sandbox Code Playgroud)

和 builtintype是完全不同的东西,它不是在里面使用的那个find