• What you talkin' 'bout??? (Was: [ksh] Readline support in a read-loop ?

    From Kenny McCormack@21:1/5 to naddy@mips.inka.de on Sat Jan 13 14:52:56 2024
    In article <slrnuq546h.20hn.naddy@lorvorc.mips.inka.de>,
    Christian Weisgerber <naddy@mips.inka.de> wrote:
    On 2024-01-13, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

    Is there a possibility to somehow get readline(3) editing support
    into such a read loop, so that whenever the prompt is displayed I
    can skim (e.g. with cursor keys) through the previous data input?

    If not in ksh; is that maybe possible in bash or zsh ?

    Not in bash, where "read -e" will use readline(3), but it doesn't
    keep any history, so you can't access previous input.

    No idea what you're on about here. It works as expected for me.

    --
    "The party of Lincoln has become the party of John Wilkes Booth."

    - Carlos Alazraqui -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Kenny McCormack on Sat Jan 13 15:05:49 2024
    On Sat, 13 Jan 2024 14:52:56 +0000, Kenny McCormack wrote:

    In article <slrnuq546h.20hn.naddy@lorvorc.mips.inka.de>,
    Christian Weisgerber <naddy@mips.inka.de> wrote:
    On 2024-01-13, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

    Is there a possibility to somehow get readline(3) editing support
    into such a read loop, so that whenever the prompt is displayed I
    can skim (e.g. with cursor keys) through the previous data input?

    If not in ksh; is that maybe possible in bash or zsh ?

    Not in bash, where "read -e" will use readline(3), but it doesn't
    keep any history, so you can't access previous input.

    No idea what you're on about here. It works as expected for me.

    I've never used this bash feature myself, so I don't have any firsthand knowledge of it.

    HOWEVER, it appears that bash(1) uses certain user-configurable
    settings[1] to enable or disable the "history" options of bash(1)'s
    "read -e"

    P'haps Christian does not have these options enabled (thus causing
    "read -e" to ignore line history), and you /do/ have them enabled
    (either by choice, or by default).

    [1] See bash(1) "READLINE" section, specifically the "Readline
    Variables" subsection, and the "Readline Initialization" subsection
    for information on how to enable/disable history.
    --
    Lew Pitcher
    "In Skills We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Kenny McCormack on Sat Jan 13 15:48:16 2024
    On 2024-01-13, Kenny McCormack <gazelle@shell.xmission.com> wrote:

    Not in bash, where "read -e" will use readline(3), but it doesn't
    keep any history, so you can't access previous input.

    No idea what you're on about here.

    What I said.

    Anybody can try it for themselves:

    while IFS= read -r -e -p "prompt> " line
    do
    echo "[$line]"
    done

    Actually, the lack of history is only true if you run it in a script.
    In an interactive shell, there is history--but it's the command
    history and it does not include the "read" input.

    Bash 5.2.21.
    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Christian Weisgerber on Sun Jan 14 02:29:11 2024
    On 13.01.2024 16:48, Christian Weisgerber wrote:
    On 2024-01-13, Kenny McCormack <gazelle@shell.xmission.com> wrote:

    Not in bash, where "read -e" will use readline(3), but it doesn't
    keep any history, so you can't access previous input.

    No idea what you're on about here.

    What I said.

    Anybody can try it for themselves:

    while IFS= read -r -e -p "prompt> " line
    do
    echo "[$line]"
    done

    Actually, the lack of history is only true if you run it in a script.
    In an interactive shell, there is history--but it's the command
    history and it does not include the "read" input.

    That would be okay (at least for ksh) since ksh's 'read -s' allows to
    drop the read input to the command history. (But ksh doesn't have the
    'read -e', OTOH. I thought the requirement would not be uncommon and
    some [emphasis!] _easy_ support would be in the shells, but no, it's
    cursed.)

    Janis, continuing later...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Janis Papanagnou on Sun Jan 14 14:08:16 2024
    On 2024-01-14, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

    That would be okay (at least for ksh) since ksh's 'read -s' allows to
    drop the read input to the command history.

    Bash also has a way to push commands into history. And in an
    _interactive_ shell, "read -e" has access to history. Also, it
    looks like the loop is considered a single command. We can combine
    all this:

    #!/usr/local/bin/bash -i
    while IFS= read -r -e -p "prompt> " line
    do
    echo "[$line]"
    history -s "$line"
    done

    It seems fragile, though, depending on underspecified behavior.
    And I lack an understanding of the consequences of forcing a shell
    to run as if interactive when executing a script.

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to naddy@mips.inka.de on Sun Jan 14 16:47:44 2024
    In article <slrnuq7qmg.2tbp.naddy@lorvorc.mips.inka.de>,
    Christian Weisgerber <naddy@mips.inka.de> wrote:
    ...
    #!/usr/local/bin/bash -i
    while IFS= read -r -e -p "prompt> " line
    do
    echo "[$line]"
    history -s "$line"
    done

    Wow. I just tried adding "-i" to my script (see other thread) - and it
    works - or does it?

    If you add -i, you get history, but the history is the script itself, not things you've previously entered at the prompt. So, that's no good.

    --
    The difference between communism and capitalism?
    In capitalism, man exploits man. In communism, it's the other way around.

    - Daniel Bell, The End of Ideology (1960) -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jim Jackson@21:1/5 to Christian Weisgerber on Sun Jan 14 18:13:20 2024
    On 2024-01-14, Christian Weisgerber <naddy@mips.inka.de> wrote:
    On 2024-01-14, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

    That would be okay (at least for ksh) since ksh's 'read -s' allows to
    drop the read input to the command history.

    Bash also has a way to push commands into history. And in an
    _interactive_ shell, "read -e" has access to history. Also, it
    looks like the loop is considered a single command. We can combine
    all this:

    #!/usr/local/bin/bash -i
    while IFS= read -r -e -p "prompt> " line
    do
    echo "[$line]"
    history -s "$line"
    done

    It seems fragile, though, depending on underspecified behavior.
    And I lack an understanding of the consequences of forcing a shell
    to run as if interactive when executing a script.


    This works for me. It's the stripped down version of a couple of scripts
    I use every login session ...

    #!/bin/bash

    HISTFILE=$HOME/.history.test
    HISTNUM=200

    while $loop ; do
    echo
    set -H -o history
    history -r
    read -p "prompt : " -e ans
    echo "$ans" >> $HISTFILE
    case "$ans" in
    q|quit|Q|Quit|QUIT)
    exit 0
    ;;
    esac
    done


    HTH
    Jim

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Jim Jackson on Sun Jan 14 19:49:45 2024
    On 14.01.2024 19:13, Jim Jackson wrote:

    This works for me. It's the stripped down version of a couple of scripts
    I use every login session ...

    It indeed looks good; I can retrieve the history entries with the arrow
    up/down keys, then navigate with the arrow left/right keys (and also
    with the Vi keys), I can even issue Vi commands, etc. - Nice. - Thanks.

    Janis

    #!/bin/bash

    HISTFILE=$HOME/.history.test
    HISTNUM=200

    while $loop ; do
    echo
    set -H -o history
    history -r
    read -p "prompt : " -e ans
    echo "$ans" >> $HISTFILE
    case "$ans" in
    q|quit|Q|Quit|QUIT)
    exit 0
    ;;
    esac
    done


    HTH
    Jim


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Jim Jackson on Sun Jan 14 18:35:35 2024
    On 2024-01-14, Jim Jackson <jj@franjam.org.uk> wrote:

    This works for me. It's the stripped down version of a couple of scripts
    I use every login session ...

    #!/bin/bash

    HISTFILE=$HOME/.history.test
    HISTNUM=200

    while $loop ; do
    echo
    set -H -o history

    Oh! That's better. We can enable history in a non-interactive
    shell and push input lines into the history:

    #!/usr/local/bin/bash
    set -o history
    while IFS= read -r -e -p "prompt> " line
    do
    echo "[$line]"
    history -s "$line"
    done

    That's the bash solution to Janis's original problem.

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)