我想设置一个返回值使其进入while循环:
#!/bin/bash
while [ $? -eq 1 ]
do
#do something until it returns 0
done
为了使其正常工作,我需要在开头设置 $? = 1
,但这样并没有起作用。
我想设置一个返回值使其进入while循环:
#!/bin/bash
while [ $? -eq 1 ]
do
#do something until it returns 0
done
为了使其正常工作,我需要在开头设置 $? = 1
,但这样并没有起作用。
你可以通过在子shell中执行带参数的exit
来设置任意退出代码。
$ (exit 42); echo "$?"
42
那么你可以这样做:
(exit 1) # or some other value > 0 or use false as others have suggested
while (($?))
do
# do something until it returns 0
done
或者你可以模拟一个do while
循环:
while
# do some stuff
# do some more stuff
# do something until it returns 0
do
continue # just let the body of the while be a no-op
done
这两种方式都可以确保循环至少运行一次,我认为这正是你的目标。
为了完整起见,exit
和return
都接受一个可选参数,这个参数是一个整数(正、负或零),它将返回值设为该整数除以256后的余数。使用 exit
退出当前 shell(或脚本或子 shell*),而使用 return
退出函数。
例如:
$ (exit -2); echo "$?"
254
$ foo () { return 2000; }; foo; echo $?
208
* 即使是由管道创建的子Shell(除非同时禁用作业控制并启用lastpipe
),这也是真实的:
$ echo foo | while read -r s; do echo "$s"; exit 333; done; echo "$?"
77
请注意最好使用break
命令来退出循环,但它的参数用于指定要退出的循环层数,而不是返回代码。
使用set +m
、set +o monitor
或shopt -u -o monitor
可以禁用作业控制。要启用lastpipe
,请执行shopt -s laspipe
。如果两者都执行了,则上面示例中的exit
将导致while
循环和包含它的 shell 同时退出,并且最终的echo
语句将不会被执行。
false
始终返回退出码1。
#!/bin/bash
false
while [ $? -eq 1 ]
do
#do something until it returns 0
done
#!/bin/bash
RC=1
while [ $RC -eq 1 ]
do
#do something until it returns 0
RC=$?
done
有些答案依赖于重新编写代码。在某些情况下,这可能是您无法控制的外来代码。
尽管对于这个具体的问题,将$?设置为1就足够了,但如果您需要将$?设置为任何值-唯一有用的答案是Dennis Williamson的答案。
一个更有效的方法是,不产生新的子进程(但也更加冗长),如下所示:
function false() { echo "$$"; return ${1:-1}; }
false 42
false 2
的行为取决于您的函数是否在环境中,所以会有不同的结果。 - dimo414false
的语义。如果您在不包括此函数的环境中使用false 42
,则会得到微妙的意外结果(可能是1
返回代码)。如果您使用不同的名称,则如果您的环境未按预期配置,您将收到清晰的错误消息。 - dimo414没有比一个简单的函数更轻巧的东西了:
function set_return() { return ${1:-0}; }
所有其他解决方案,例如(...)
或[...]
或false
可能包含外部进程调用。
虽然这个问题比较老,但是现在有一个更好的答案:
#!/bin/bash
until
#do something until it returns success
do
:;
done
如果您正在循环等待某些操作成功,那么只需在“直到”部分执行该操作即可。您可以将完全相同的代码放在“直到”部分中,而不是在“执行/完成”部分中编写代码,然后将其结果传输回while或until。
$?
可以包含 0 到 255 的字节值。返回超出此范围的数字将被重新映射到此范围,就像应用了按位与 255。
退出值
- 可用于设置值,但很严格,因为它将终止进程/脚本。
返回值
- 在函数中使用略有传统意义。
[[ ... ]]
- 适用于求解布尔表达式。
以下是一个exit
的示例:
# Create a subshell, but, exit it with an error code:
$( exit 34 ); echo $? # outputs: 34
Here are examples of return
:
# Define a `$?` setter and test it:
set_return() { return $1; }
set_return 0; echo $? # outputs: 0
set_return 123; echo $? # outputs: 123
set_return 1000; echo $? # outputs: 232
set_return -1; echo $? # outputs: 255
[ ... ]
的示例:# Define and use a boolean test:
lessthan() { [[ $1 < $2 ]]; }
lessthan 3 8 && echo yes # outputs: yes
lessthan 8 3 && echo yes # outputs: nothing
注意,使用$?
作为条件语句时,零(0)表示成功,非零表示失败。
这个东西是你在找的吗?
#!/bin/bash
TEMPVAR=1
while [ $TEMPVAR -eq 1 ]
do
#do something until it returns 0
#construct the logic which will cause TEMPVAR to be set 0 then check for it in the
#if statement
if [ yourcodehere ]; then
$TEMPVAR=0
fi
done
这是我正在使用的
allow_return_code() {
local LAST_RETURN_CODE=$?
if [[ $LAST_RETURN_CODE -eq $1 ]]; then
return 0
else
return $LAST_RETURN_CODE
fi
}
# it converts 2 to 0,
my-command-returns-2 || allow_return_code 2
echo $?
# 0
# and it preserves the return code other than 2
my-command-returns-8 || allow_return_code 2
echo $?
# 8
$(exit 42); echo "$?"
。 - Elliot Chance