bash -
以下bash shell代码是什么意思?它似乎用于将输出的最后一个代码作为输入。如果是这样,我可以只写为bash
还是xargs bash
?
curl --silent --location https://rpm.nodesource.com/setup | bash -
如有疑问,请阅读源代码。=)
Bash 4.3,shell.c
第830行,功能parse_shell_options()
如下:
/* A single `-' signals the end of options. From the 4.3 BSD sh.
An option `--' means the same thing; this is the standard
getopt(3) meaning. */
if (arg_string[0] == '-' &&
(arg_string[1] == '\0' ||
(arg_string[1] == '-' && arg_string[2] == '\0')))
return (next_arg);
换句话说,这-
就是说没有更多选择了。如果命令行上还有其他单词,即使该单词以a开头,它们也将被视为文件名-
。
当然,在您的示例中,这-
完全是多余的,因为无论如何都没有遵循它。换句话说,bash -
与完全等效bash
。
Bash接受命令
这是一种误解,bash -
告诉Bash从其标准输入中读取其命令。虽然这是事实,在你的榜样,bash将标准输入读取它的命令,就已经这样做了,无论是否有一个-
命令行上,因为,如上所述,bash -
是相同的bash
。
为了进一步说明这-
并不意味着stdin,请考虑:
该cat
命令旨在将a解释-
为stdin。例如:
$ echo xxx | cat /etc/hosts - /etc/shells
127.0.0.1 localhost
xxx
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/bin/zsh
/usr/bin/zsh
/usr/bin/screen
/bin/tcsh
/usr/bin/tcsh
/usr/bin/tmux
/bin/ksh93
相反,你不能击来执行/bin/date
,然后/bin/hostname
通过尝试这样的:
$ echo date | bash - hostname
/bin/hostname: /bin/hostname: cannot execute binary file
而是尝试将其解释/bin/hostname
为shell脚本文件,该文件失败,因为它是一堆二进制gobbledygook。
您不能date +%s
使用bash -
两者之一执行。
$ date +%s
1448696965
$ echo date | bash -
Sat Nov 28 07:49:31 UTC 2015
$ echo date | bash - +%s
bash: +%s: No such file or directory
你能写xargs bash
吗?否。curl | xargs bash
将使用脚本内容作为命令行参数来调用bash。内容的第一个单词将是第一个参数,并且可能会被误解为脚本文件名。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句