Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在bg中启动时,wget cmdline返回代码不正确_Bash_Shell_Wget - Fatal编程技术网

Bash 在bg中启动时,wget cmdline返回代码不正确

Bash 在bg中启动时,wget cmdline返回代码不正确,bash,shell,wget,Bash,Shell,Wget,我在wget命令行中指定了错误的url,并在bg中启动了该命令: $ wget -q -O ubuntu.img http://cdffimage.ubuntu.com/precise/dvd/current/precise-dvd-i386.iso & [1] 18039 $ echo $? 0 [1]+ Exit 4 wget -q -O ubuntu.img http://cdffimage.ubuntu.com/precise/dvd/curr

我在wget命令行中指定了错误的url,并在bg中启动了该命令:

$ wget -q -O ubuntu.img http://cdffimage.ubuntu.com/precise/dvd/current/precise-dvd-i386.iso &
[1] 18039
$ echo $?
0
[1]+  Exit 4                  wget -q -O ubuntu.img http://cdffimage.ubuntu.com/precise/dvd/current/precise-dvd-i386.iso

我的问题是,为什么我从
echo$?
获得
0
的返回码?由于发生错误,我应该得到返回代码
4

您正在后台运行该命令。当你的外壳恢复控制时,它还没有完成。它不能为您提供尚未完成的流程的返回代码

另外,
$?
被记录为(在bash手册页中):


除了使用
wait
等待后台作业外,我不知道如何通过编程方式接收后台作业的退出代码。

即使在等待命令失败后,我也会得到0的rc。你能在你这边试试吗?@abc就像我说的,一旦你在后台运行命令,我不相信你可以在不使用
wait
的情况下获得退出状态,从而有效地停止在后台执行该作业/任务。后台作业在某种程度上与当前shell“分离”。如果它在后台退出,
$?
将永远不会给你它的退出状态(我不相信除了显示的消息之外的任何东西都会给你)。@abc或者你的意思是即使你不把它放在后台?可能是重复的
$?      Expands to the status of the most recently executed foreground pipeline.