man man
页面摘录:
默认操作是按照预定义的顺序搜索所有可用部分(默认为“1 nl 8 3 2 3posix 3pm 3perl 5 4 9 6 7”)
手册的n
,l
和3pm
部分是什么?
支撑扩展,例如 {a,b,c}不是由 POSIX 定义的。我想在 POSIX 模式下运行我的 shell。使用 Debian 这很简单:
$ bash -c 'echo {a,b,c}'
a b c
$ sh -c 'echo {a,b,c}'
{a,b,c}
Run Code Online (Sandbox Code Playgroud)
但是 Fedora 的行为有所不同:
$ bash -c 'echo {a,b,c}'
a b c
$ sh -c 'echo {a,b,c}'
a b c
Run Code Online (Sandbox Code Playgroud)
我尝试使用--posix
选项,但没有效果:
$ sh --posix -c 'echo {a,b,c}'
a b c
Run Code Online (Sandbox Code Playgroud)
Bash 可以强制在 POSIX 模式下运行吗?
在 BSD sed 上,-E
是“扩展正则表达式”标志。在 GNU sed 上,文档指出这-r
是扩展的正则表达式标志,但-E
开关也能正常工作(尽管在我的研究中没有记录)。
我记得读过-E
将在下一版 POSIX 规范中指定的某个地方,但我找不到我在哪里读到的。
(这是真的吗?是否有权威参考,或者这里的用户是权威?)
究竟有多便携是在-E
为开关sed
?
是否有标准(即POSIX兼容)的版本sed
上-E
是不支持?(哪个?)
为什么-E
没有为 GNU sed 记录该标志?
我一直在寻找Kusalananda和xhienne之间的讨论在这里,在这里它提到[ "" -ge 2 ]
不是一个有效的测试产生的错误bash --posix
和其他POSIX兼容的炮弹。
bash-4.3$ [ "" -gt 10 ]
bash: [: : integer expression expected
bash-4.3$ [ '' -gt 10 ]
bash: [: : integer expression expected
Run Code Online (Sandbox Code Playgroud)
那里一切都很好。出于好奇,我对[[
.
bash-4.3$ [[ "" -gt 10 ]] && echo "YES"
bash-4.3$ [[ "" -gt 0 ]] && echo "YES"
bash-4.3$ [[ "" -gt -1 ]] && echo "YES"
YES
bash-4.3$ [[ "" -eq 0 ]] && echo "YES"
YES
Run Code Online (Sandbox Code Playgroud)
如您所见,没有错误,它实际上被评估为数字表达式,其中 "" 等于 …
如何在 POSIX shell 脚本中使用伪数组?
我想,以取代10个整数数组与bash脚本的东西相似到POSIX shell脚本。
在使用数组部分,我设法遇到了Rich 的 sh (POSIX shell) 技巧。
我试过的:
save_pseudo_array()
{
for i do
printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/"
done
echo " "
}
coords=$(save_pseudo_array "$@")
set -- 1895 955 1104 691 1131 660 1145 570 1199 381
eval "set -- $coords"
Run Code Online (Sandbox Code Playgroud)
我不明白,这就是问题所在,如果有人能对此有所了解,非常感谢。
标准实用程序的 Linux 基础列表包括getopts
但不包括getopt
. Posix 实用程序的Open Group列表类似。
同时,维基百科的标准 Unix 命令列表包括getopt
但不包括getopts
. 同样,Windows Subsystem for Linux(基于 Ubuntu 基于 Debian)也包括getopt
但不包括getopts
(它是GNU 增强版)。
balter@spectre:~$ which getopt
/usr/bin/getopt
balter@spectre:~$ getopt -V
getopt from util-linux 2.27.1
balter@spectre:~$ which getopts
balter@spectre:~$
Run Code Online (Sandbox Code Playgroud)
因此,如果我想选择一个我最有信心使用更标准 Linux 发行版(例如 Debian、Red Hat、Ubuntu、Fedora、CentOS 等)的人,我应该选择哪个?
感谢 Michael 和 Muru 解释了内置与可执行文件。我刚刚也偶然发现了这个,其中列出了 bash 内置函数。
如何用单引号引用字符串?
例如,我可以这样做:
$ printf "%q\n" 'two words'
two\ words
$
Run Code Online (Sandbox Code Playgroud)
有没有办法得到一个单(或双)引用的字符串作为输出,即:
$ MAGIC 'two words'
'two words'
$
Run Code Online (Sandbox Code Playgroud)
我发现单引号版本更容易阅读。
我想要一个适用于 {ba,z}sh 的答案。POSIX shell 将是一个奖励。
只有read -r
是通过POSIX规定; read -n NUM
,用于读取NUM
字符,不是。从标准输入读取给定数量的字符后,是否有一种可移植的方式自动返回?
我的用例正在打印这样的提示:
Do the thing? [y/n]
Run Code Online (Sandbox Code Playgroud)
如果可能,我希望程序在键入后自动运行y
或n
,而无需用户随后按 Enter 键。
我知道 dash 非常严格地遵守 POSIX 标准,但我知道它不是 100.00% 严格的 POSIX,没有任何额外的东西。据我所知,最接近/完全遵守 POSIX 标准的 shell 是设置了 POSIXly 正确标志的 mrsh 或 yash。
现在我想确切地知道 POSIX 标准中没有指定 dash 的哪些部分/功能(无需阅读整个POSIX.1-2017和 dash 的源代码)。
我已经尝试过广泛的谷歌搜索(就我对这个主题的了解而言),但谁会想到,这完全是关于 bash 和 dash 之间的差异以及 dash 如何非常符合 POSIX 等等的结果。
我想使用2.6.2 参数扩展从字符串中删除前导字符,但惊讶地发现“删除最大前缀模式”不会自动重复该模式。
$ x=aaaaabc
$ printf %s\\n "${x##a}"
aaaabc
Run Code Online (Sandbox Code Playgroud)
如您所见,只有第一个a
已被删除。预期输出为、、或中bc
的任何一个。x=bc
x=abc
x=aabc
x=aaabc
x=aaaabc
a
如果我想从 的开头删除尽可能多的内容,我正在努力弄清楚如何编写该模式$x
。我也没有运气搜索其他线程,因为许多答案都使用 bash,但我正在寻找 POSIX shell 解决方案。