mainThe GNU Bourne-Again SHell - Support: sr #110282, read command terminating silently...

 
 

sr #110282: read command terminating silently upon reading from heredoc

Submitter:  Ganapathi Kamath <hgkamath>
Submitted:  Sun 26 Jul 2020 04:15:42 PM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  Works For Me
Privacy:  Public Assigned to:  None
Open/Closed:  Open Operating System:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Tue 04 Aug 2020 07:29:11 PM UTC, comment #7: 


Understood,
Yes, those words are right up there in the man-page, but easy to miss if one is looking for an argument-leader and a description.
I just learnt something about bash options today. Thankyou

if all seems well by you, you may close this non-bug

Ganapathi Kamath <hgkamath>
Tue 04 Aug 2020 07:22:10 PM UTC, comment #6: 

It is the same as what's described for the `set' builtin.

"All of the single-character shell options documented in the
 description of the set builtin command, including -o, can be
 used as options when the  shell  is invoked."

Chet Ramey <chet>
Group administrator
Tue 04 Aug 2020 07:04:29 PM UTC, comment #5: 


https://stackoverflow.com/questions/9952177/whats-the-meaning-of-the-parameter-e-for-bash-shell-command-line/9952249

In the manpage, it seems to be the same as what is mentioned for the "set" command options.

Ganapathi Kamath <hgkamath>
Tue 04 Aug 2020 07:02:14 PM UTC, comment #4: 

-e      Exit  immediately  if a pipeline (which may consist of a
        single simple command), a list, or  a  compound  command
        (see SHELL GRAMMAR above), exits with a non-zero status.
        The shell does not exit if the  command  that  fails  is
        part  of  the command list immediately following a while
        or until keyword, part of the test following the  if  or
        elif  reserved  words, part of any command executed in a
        && or || list except the command following the final  &&
        or ||, any command in a pipeline but the last, or if the
        command's return value is being inverted with !.   If  a
        compound  command  other  than a subshell returns a non-
        zero status because a command failed while -e was  being
        ignored,  the  shell  does  not exit.  A trap on ERR, if
        set, is executed before the shell  exits.   This  option
        applies to the shell environment and each subshell envi-
        ronment separately (see  COMMAND  EXECUTION  ENVIRONMENT
        above), and may cause subshells to exit before executing
        all the commands in the subshell.

        If a compound command or shell function  executes  in  a
        context  where -e is being ignored, none of the commands
        executed within the compound command  or  function  body
        will  be  affected  by the -e setting, even if -e is set
        and a command returns a failure status.  If  a  compound
        command  or  shell function sets -e while executing in a
        context where -e is ignored, that setting will not  have
        any  effect  until  the  compound command or the command
        containing the function call completes.

Chet Ramey <chet>
Group administrator
Tue 04 Aug 2020 06:53:45 PM UTC, comment #3: 


comment #2:

> I can't reproduce this using bash-4.4, bash-5.0, or bash-5.1-alpha on macOS or RHEL 7.



After your comment, I narrowed down something:

If I run the script as
0) directly on the interactive shell prompt
  all 4 examples work
1) as executable-file from shell
  ./a.sh  
  all 4 examples work
2) with debug/traced executation
  bash -x ./a.sh
  all 4 examples work
3) when run as a command string
  bash -c a.sh
  all 4 examples work
4)
  bash a.sh
  all 4 examples work
5)
  bash -e a.sh
  only the first 2 examples work

I was running the script in the 5fth way, in both windows and linux. Repeated the above for both OS-es, same result. I was originally writing the script in windows and hence was using running the scripts by executing bash with arguments.

I thought -e was a synonym for -c.
I looked at the man page, and I am beginning to wonder if -e is a  stray option.

What does -e do?

Ganapathi Kamath <hgkamath>
Mon 03 Aug 2020 02:40:47 PM UTC, comment #2: 

I can't reproduce this using bash-4.4, bash-5.0, or bash-5.1-alpha on macOS or RHEL 7.

Chet Ramey <chet>
Group administrator
Sun 26 Jul 2020 04:21:38 PM UTC, comment #1: 

Typo mismatches in bash versions

- win10 -> VirtualBox-VM-Linux -> Fedora-32 -> console-tty -> bash GNU bash, version 5.0.17(1)-release (x86_64-redhat-linux-gnu)
- win10 -> Wezterm -> ssh-8.1 to LinuxVM-> sshd8.3 (Linux) -> bash GNU bash, version 5.0.17(1)-release (x86_64-redhat-linux-gnu)
- win10 -> git-bash-mintty -> bash GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
- win10 -> pwsh-7.0.3 -> bash GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
- win10 -> Wezterm -> pwsh-7.0.3 -> bash GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
- win10 -> hyperjs -> pwsh-7.0.3 -> bash GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)

Ganapathi Kamath <hgkamath>
Sun 26 Jul 2020 04:15:42 PM UTC, original submission:  

Subject: read command terminating silently upon reading from heredoc

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -Wno-parentheses -Wno-format-security
uname output: Linux fedoraminimalm13 5.7.8-200.fc32.x86_64 #1 SMP Thu Jul 9 14:34:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-redhat-linux-gnu

Bash Version: 5.0
Patch Level: 17
Release Status: release

Description:
Copy/Paste the following lines (between triple) quotes to inside shell scripts a1.sh, a2,sh, a3.sh, a4.sh
```
define(){ IFS='\n' read -r -d '' ${1} || true; }
define VAR1 <<'ENDT'
 abc'asdf"
$(dont-execute-this)

foo"bar"''
ENDT
printf "AAR1\n${VAR1}BAR1\n"

{
IFS='\n' read -r -d '' VAR2 << 'ENDT'
 abc'asdf"
$(dont-execute-this)

foo"bar"''
ENDT
} || true ;
printf "AAR2\n${VAR2}BAR2\n"

IFS='\n' read -r -d '' VAR3 << 'ENDT'
 abc'asdf"
$(dont-execute-this)

foo"bar"''
ENDT
printf "AAR3\n${VAR3}BAR3\n"

IFS='\n' read -r -d '' VAR4 <<<'abc'"'"'asdf"'"\n"'$(dont-execute-this)'"\n\n"'foo"bar"'"''\n" ; printf "AAR4\n${VAR4}BAR4\n"
```
All 4 examples work on the command line
The last two examples do not work inside a script.
It is not clean what the "{} || true" have to do with making the read command work
# rpm -qa | grep bash-5
bash-5.0.17-1.fc32.x86_64

Repeat-By:
If typed directly in interactive shell mode, all 4 scriptlets work as expected.
However, when each script is run in batch infocation mode, a3.sh and a4.sh do not get to the subsequent printf statement and show output
Batch script terminates at read silently.
If the "{} || true is required for some reason", then that behavior is not documented in the man page 'read [' section
The effect is same in
- win10 -> Wezterm -> ssh-8.1 -> sshd8.3 -> bash GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
- win10 -> git-bash-mintty -> bash GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
- win10 -> pwsh-7.0.3 -> bash GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
- win10 -> VirtualBox-VM-Linux -> Fedora-32 -> console-tty -> bash GNU bash, version 5.0.17(1)-release (x86_64-redhat-linux-gnu)
- win10 -> Wezterm -> pwsh-7.0.3 -> bash GNU bash, version 5.0.17(1)-release (x86_64-redhat-linux-gnu)
- win10 -> hyperjs -> pwsh-7.0.3 -> bash GNU bash, version 5.0.17(1)-release (x86_64-redhat-linux-gnu)

Fix:
Basically, the intent was to raw read a heredoc inside a shell script.
Workaround: For that purpose just use the "{} || true" to make the read heredoc work as expected.

Ganapathi Kamath <hgkamath>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by hgkamath (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-08-03 chet StatusNone Works For Me

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code