背景
虽然zsh 文档(第 6.2 章)提供了关于precommand 修饰符可以做什么的描述,但它没有提供每种类型的示例:-
、builtin
、command
、exec
、nocorrect
和noglob
.
用户Tuyen Pham也提出了类似的问题。但是,该问题并未强调为每种类型的前置命令修饰符请求示例。
问题
任何人都可以提供每个前置命令修饰符可以做什么的例子吗?
干杯!
虽然zsh 文档(第 6.2 章)提供了关于precommand 修饰符可以做什么的描述,但它没有提供每种类型的示例:-
、builtin
、command
、exec
、nocorrect
和noglob
.
用户Tuyen Pham也提出了类似的问题。但是,该问题并未强调为每种类型的前置命令修饰符请求示例。
任何人都可以提供每个前置命令修饰符可以做什么的例子吗?
干杯!
#!/bin/sh
foo() {
echo "in foo"
}
type foo
checkbashisms.pl
明明不喜欢type
,为什么?
$ checkbashisms.pl foo.sh
possible bashism in foo.sh line 7(type):
type foo
不是POSIX吗?但是所有常见的shell都支持它(即bash
, zsh
, dash
, busybox sh
, mksh
; 即使在ksh
; 也许只是csh
不支持它),难道没有办法抑制这个警告吗?
如果我跑
nrolland@mactoasty ~ $ type -p skhd
skhd is /usr/local/bin/skhd
我不能用其他命令很好地组合它,比如
nrolland@mactoasty ~ $ la `type -p skhd`
ls: is: No such file or directory
ls: skhd: No such file or directory
lrwxr-xr-x 1 nrolland admin 29B Jun 4 09:35 /usr/local/bin/skhd -> ../Cellar/skhd/0.2.2/bin/skhd
只获得第二部分的最干净的方法是什么?(如果有帮助,我正在使用 zsh)
当内置函数在查找内置函数时具有完全相同的功能时,内置函数的builtin
意义何在?command
当确保选择特定内置函数而不是同名函数时,builtin
与command
. 有人可能会争辩说这builtin
更明确,但我只能想象,command
当不确定某些命令是否也可用作内置命令时,可能会选择 using。
摘要:如果bash
我尝试将缺失函数的输出分配给先前的declare
d(即,不是 {constant,read-only})变量,我可以通过“正常”测试检测到失败。但是,如果我在对它进行操作时尝试将缺失函数的输出分配给一个变量declare
(例如,使 var {constant, read-only}),不仅分配不会因“正常”测试而失败,而且我不能使用“正常”内置函数强制失败。我怎样才能使后一种情况失败?
细节:
我最近在一个更大的bash
脚本中遇到了一个问题,我试图将其提炼成以下 2 个脚本。基本上,我有点像用bash
( snark > /dev/null
) 做 TDD,所以除其他外
但是,bash
似乎允许我在declare
输入 var 时将缺失函数的输出分配给变量。例如,以下脚本(另存为/path/to/assign_at_declare.sh
)...
#!/usr/bin/env bash
function foo() {
return 0
}
# function bar() {} # does not exist!
declare ret_val=''
declare -r MESSAGE_PREFIX="$(basename "${BASH_SOURCE}"):"
declare -r ERROR_PREFIX="${MESSAGE_PREFIX} ERROR:"
echo -e "\n${MESSAGE_PREFIX} a naïve 1st attempt:\n"
declare -ir FOO1_VAL="$(foo)" # this should succeed, and does
ret_val="${?}"
if [[ "${ret_val}" -ne 0 ]] ; then
>&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
exit 3
elif [[ -z "${FOO1_VAL}" ]] ; then
>&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
exit 4
else
echo "${MESSAGE_PREFIX} FOO1_VAL='${FOO1_VAL}'"
fi
declare -ir BAR1_VAL="$(bar)" # this should fail ... but doesn't
ret_val="${?}"
if [[ "${ret_val}" -ne 0 ]] ; then
>&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
exit 5
elif [[ -z "${BAR1_VAL}" ]] ; then
>&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
exit 6
else
echo "${MESSAGE_PREFIX} BAR1_VAL='${BAR1_VAL}'"
fi
echo -e "\n${MESSAGE_PREFIX} get tough using \`set\` builtins:\n"
# see https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
set -o errexit
set -o pipefail
declare -ir FOO2_VAL="$(foo)" # this should succeed, and does
ret_val="${?}"
if [[ "${ret_val}" -ne 0 ]] ; then
>&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
exit 3
elif [[ -z "${FOO2_VAL}" ]] ; then
>&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
exit 4
else
echo "${MESSAGE_PREFIX} FOO2_VAL='${FOO2_VAL}'"
fi
declare -ir BAR2_VAL="$(bar)" # this should fail ... but doesn't
ret_val="${?}"
if [[ "${ret_val}" -ne 0 ]] ; then
>&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
exit 5
elif [[ -z "${BAR2_VAL}" ]] ; then
>&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
exit 6
else
echo "${MESSAGE_PREFIX} BAR2_VAL='${BAR2_VAL}'"
fi
exit 0
...产生以下输出:
assign_at_declare.sh: a naïve 1st attempt:
assign_at_declare.sh: FOO1_VAL='0'
/path/to/assign_at_declare.sh: line 27: bar: command not found
assign_at_declare.sh: BAR1_VAL='0'
assign_at_declare.sh: get tough using `set` builtins:
assign_at_declare.sh: FOO2_VAL='0'
/path/to/assign_at_declare.sh: line 56: bar: command not found
assign_at_declare.sh: BAR2_VAL='0'
这看起来很奇怪,因为如果我在输入 var后 declare
尝试将缺失函数的输出分配给变量(即,如果 var不是{constant, read-only}) ,我没有观察到这种行为,如以下脚本所示(另存为/path/to/assign_after_declare.sh
)...
#!/usr/bin/env bash
function foo() {
return 0
}
# function bar() {} # does not exist!
declare ret_val=''
declare -i foo_val=0
declare -i bar_val=0
declare -r MESSAGE_PREFIX="$(basename "${BASH_SOURCE}"):"
declare -r ERROR_PREFIX="${MESSAGE_PREFIX} ERROR:"
echo -e "\n${MESSAGE_PREFIX} following works as expected\n"
foo_val="$(foo)" # this should succeed, and does with/out `declare`
ret_val="${?}"
if [[ "${ret_val}" -ne 0 ]] ; then
>&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
exit 3
elif [[ -z "${foo_val}" ]] ; then
>&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
exit 4
else
echo "${MESSAGE_PREFIX} foo_val='${foo_val}'"
fi
bar_val="$(bar)" # this succeeds with `declare`, fails without
ret_val="${?}"
if [[ "${ret_val}" -ne 0 ]] ; then
>&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
exit 5
elif [[ -z "${bar_val}" ]] ; then
>&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
exit 6
else
echo "${MESSAGE_PREFIX} bar_val='${bar_val}'"
fi
exit 0
...产生以下输出:
assign_after_declare.sh: following works as expected
assign_after_declare.sh: foo_val='0'
/path/to/assign_after_declare.sh: line 29: bar: command not found
assign_after_declare.sh: ERROR: bar returned '127', exiting ...
在 a 期间分配时有没有办法强制bash
快速失败?如果是这样,我等待您的答复。declare
或者,是否bash
按设计工作?如果是这样,请链接到参考资料;我试着搜索这个问题,但我的选择器要么不正确,要么返回了太多不相关的响应,以至于毫无用处。