mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-28 09:31:16 -03:00
Make while loops evaluate to the last executed command status
A while loop now evaluates to the last executed command in the body, or zero if the loop body is empty. This matches POSIX semantics. Add a bunch of tricky tests. See #4982
This commit is contained in:
@@ -18,10 +18,56 @@ function runs_once
|
||||
end
|
||||
|
||||
# this should return 1
|
||||
never_runs; echo $status
|
||||
never_runs; echo "Empty Loop in Function: $status"
|
||||
|
||||
# this should return 0
|
||||
runs_once; echo $status
|
||||
runs_once; echo "Runs Once: $status"
|
||||
|
||||
# this should return 2
|
||||
early_return; echo $status
|
||||
early_return; echo "Early Return: $status"
|
||||
|
||||
logmsg Loops exit status handling
|
||||
|
||||
function set_status ; return $argv[1]; end
|
||||
|
||||
# The previous status is visible in the loop condition.
|
||||
# This includes both the incoming status, and the last command in the
|
||||
# loop body.
|
||||
set_status 36
|
||||
while begin
|
||||
set -l saved $status
|
||||
echo "Condition Status: $status"
|
||||
set_status $saved
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
# The condition status IS visible in the loop body.
|
||||
set_status 55
|
||||
while true
|
||||
echo "Body Status: $status"
|
||||
break
|
||||
end
|
||||
|
||||
# The status of the last command is visible in the loop condition
|
||||
set_status 13
|
||||
while begin
|
||||
set -l saved $status
|
||||
echo "Condition 2 Status: $saved"
|
||||
test $saved -ne 5
|
||||
end
|
||||
set_status 5
|
||||
end
|
||||
|
||||
# The status of the last command is visible outside the loop
|
||||
set rem 5 7 11
|
||||
while [ (count $rem) -gt 0 ]
|
||||
set_status $rem[1]
|
||||
set rem $rem[2..-1]
|
||||
end
|
||||
echo "Loop Exit Status: $status"
|
||||
|
||||
# Empty loops succeed.
|
||||
false
|
||||
while false; end
|
||||
echo "Empty Loop Status: $status"
|
||||
|
||||
Reference in New Issue
Block a user