Discussion:
suspend and ||
e***@gsyc.urjc.es
2018-09-21 11:49:21 UTC
Permalink
According to the manual:

(I) Typing the suspend character (typically ^Z, Control-Z)
while a process is running causes that process to be
stopped and returns control to bash.

(II) An OR list has the form

command1 || command2

command2 is executed if and only if command1
returns a non-zero exit status. The return status of
AND and OR lists is the exit status of the last
command executed in the list.

In the following example:

***@omac:~$ sleep 10 || echo hey
^Z
[5]+ Stopped sleep 10
hey
***@omac:~$

there are two different issues:

(I) echo is executed when sleep is suspended, i.e., command2 is
executed before command1 exits.

(II) the exit status of command1 (when the job is resumed) will
be 0 and command2 is (was) executed.

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I../. -I.././include -I.././lib -Wdate-time -D_FORTIFY_SOURCE=2 -g -O2 -fdebug-prefix-map=/build/bash-vEMnMR/bash-4.4.18=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -Wno-parentheses -Wno-format-security
uname output: Linux omac 4.15.0-34-generic #37-Ubuntu SMP Mon Aug 27 15:21:48 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.4
Patch Level: 19
Release Status: release

Description:
[Detailed description of the problem, suggestion, or complaint.]

Repeat-By:
[Describe the sequence of events that causes the problem
to occur.]

Fix:
[Description of how to fix the problem. If you don't know a
fix for the problem, don't include this section.]
Chet Ramey
2018-09-21 15:54:33 UTC
Permalink
Post by e***@gsyc.urjc.es
(I) Typing the suspend character (typically ^Z, Control-Z)
while a process is running causes that process to be
stopped and returns control to bash.
(II) An OR list has the form
command1 || command2
command2 is executed if and only if command1
returns a non-zero exit status. The return status of
AND and OR lists is the exit status of the last
command executed in the list.
^Z
[5]+ Stopped sleep 10
hey
(I) echo is executed when sleep is suspended, i.e., command2 is
executed before command1 exits.
This is true, and it's the difference between a process, which is the
object you suspend, and a shell command. When you suspend the sleep,
it returns a status: 128+SIGTSTP. Since this is non-zero, the second
part of the AND-OR list gets executed.

The idiomatic way to do what you want is to get the command you want
to run as a unit into a construct like (...) -- if you want to run it
in the foreground -- so you have a single process that you can suspend.

This has come up many times, in the context of compound commands like
loops and command sequences.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
Loading...