bash 手册指出:
eval [arg ...]
The args are read and concatenated together into a single com-
mand. This command is then read and executed by the shell, and
its exit status is returned as the value of eval. If there are
no args, or only null arguments, eval returns 0.
Run Code Online (Sandbox Code Playgroud)
我试试
eval `nonsense`
echo $?
Run Code Online (Sandbox Code Playgroud)
结果是0。
而当我单独执行反引号命令时:
`nonsense`
echo $?
Run Code Online (Sandbox Code Playgroud)
结果是127。
从什么是写在bash的手册我希望eval以回报127服用后引号时nonsense作为参数。
如何获取参数的退出状态eval?
如何提取命令的退出代码并在条件中使用它?例如看下面的脚本:
i=4
if [[ $i -eq 4 && $(command) -eq 0 ]]
then
echo "OK"
else
echo "KO"
fi
Run Code Online (Sandbox Code Playgroud)
如果这两个条件都满足,脚本应该做一些事情。在第二种情况下,我需要检查退出状态(而不是上面给出的脚本中的命令输出),以了解命令是否成功。
我有一个 bash 脚本,它只是 docker 推送图像:
docker push $CONTAINER_IMAGE:latest
Run Code Online (Sandbox Code Playgroud)
当这失败时,我想循环 3 次。我应该如何实现这一目标?
假设我正在编写自己的在 Linux 内核上运行的 init 程序。
当我的 init 程序以返回值 0 退出时会发生什么?
此外,如果返回值非零,行为是否不同?
是否可以$?在测试后保持最后一个命令退出状态 ( ) 不变?
例如,我想做:
command -p sudo ...
[ $? -ne 1 ] && exit $?
Run Code Online (Sandbox Code Playgroud)
最后一个exit $?应该返回 sudo 退出状态,但它总是返回0(测试的退出代码)。
没有临时变量可以做到这一点吗?
另一个进一步澄清的例子:
spd-say "$@"
[ $? -ne 127 ] && exit $?
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我只想在找到第一个命令时退出(退出代码 != 127)。我想用实际的spd-say退出代码退出(可能不是
0)。
编辑:我忘了提到我更喜欢 POSIX-complaint 解决方案以获得更好的可移植性。
我在脚本中使用这个构造,我想为相同的命令提供替代方案。例如,请参阅我的crc32 脚本。
临时变量的问题在于它们可能会影响其他变量,并且为了避免必须使用长名称,这对代码可读性不利。
This question is similar to the following link, but focused on using the command line (bash shell).
Using a simple example, when doing the following command:
$ cat <(date); echo $?
Fri Jul 7 21:04:38 UTC 2017
0
Run Code Online (Sandbox Code Playgroud)
The exit value is 0 as expected.
In the following command there is an error introduced on purpose, but the return value is still 0:
$ cat <(datE); echo $?
bash: datE: command not found...
Similar command is: 'date'
0
Run Code Online (Sandbox Code Playgroud)
Is …
跑步
bash -c 'bash -c "echo test1; exit 1;" &> /tmp/x; buildresult=$?; tail -n 100 /tmp/x; exit $buildresult;'
Run Code Online (Sandbox Code Playgroud)
结果test1被打印到控制台并echo $?打印1在我的理解中是正确的,因为命令应该返回内部[b/d]ash -c返回的内容,而
dash -c 'dash -c "echo test1; exit 1;" &> /tmp/x; buildresult=$?; tail -n 100 /tmp/x; exit $buildresult;'
Run Code Online (Sandbox Code Playgroud)
导致相同的输出,但0根据返回echo $?。
我想了解这种差异,以拓宽我对 shell 和可移植 shell 编程的理解。
我在 Ubuntu 17.10 (Artful Aardvark) 上使用bash4.4.12 和dash0.5.8-2.3ubuntu1。
当状态码无用时,有没有办法根据stdout的输出构造一个管道?
我希望答案不是针对用例,而是针对 shell 脚本范围内的问题。我想要做的是通过根据国家和语言代码猜测名称来找到存储库中可用的最具体的包。
以这个为例,
$PACKAGE1=hunspell-en-zz$PACKAGE2=hunspell-en第一种猜测更合适,但它可能不存在。在这种情况下,我想回到hunspell-en($PACKAGE2),因为第一个选项hunspell-en-zz($PACKAGE1)也不会存在。
apt-cache只要命令能够运行(来自 的文档apt-cache),该命令就会返回成功(由 shell 定义为退出代码零)
apt-cache 在正常操作时返回零,在错误时返回十进制 100。
这使得在管道中使用命令变得更加困难。通常,我希望包搜索等效于 404 会导致错误(就像curl或会发生的那样wget)。我想搜索以查看包是否存在,如果不存在则回退到另一个包。
这不返回任何内容,因为第一个命令返回成功(因此||永远不会运行中的 rhs )
apt-cache search hunspell-en-zz || apt-cache search hunspell-en
Run Code Online (Sandbox Code Playgroud)
apt-cache search 有两个论点这不返回任何内容,因为apt-cacheAND 其参数,
apt-cache search hunspell-en-zz hunspell-en
Run Code Online (Sandbox Code Playgroud)
从文档 apt-cache
可以使用单独的参数来指定多个搜索模式,这些模式是和'ed 在一起的。
因此,由于这些参数之一显然不存在,因此不会返回任何内容。
处理诸如apt-cache返回代码对任务无用的约定的 shell 习语是什么?成功仅取决于 STDOUT 上是否存在输出?
他们都源于同一个问题。那里提到的选择的答案
find -z …
在我的终端中,它打印出一个看似随机的数字127。我认为它正在打印一些变量的值并检查我的怀疑,我定义了一个新变量v=4。echo $?再次运行给了我0作为输出。
我很困惑,因为我期待 4 是答案。
exit-status ×10
bash ×6
shell ×3
shell-script ×2
apt ×1
command-line ×1
dash ×1
debian ×1
docker ×1
eval ×1
exit ×1
for ×1
init ×1
linux ×1
linux-kernel ×1
math ×1
pipe ×1
subshell ×1
variable ×1