• Re: echoing the last command inside bash function

    From Kenny McCormack@21:1/5 to J.O. Aho on Thu Jun 30 12:04:45 2022
    XPost: alt.os.linux

    In article <[email protected]>,
    J.O. Aho <[email protected]> wrote:
    ...
    Now instead !! is not rightly evaluated.
    Why?

    For in script you have the history expand by default disabled.

    If you want it to be like in command line, then you need to enable the >history expand

    --- bash script ---
    #!/bin/bash
    set -o history -o histexpand

    ls /tmp > /dev/null
    echo "CMD='"!:0"'"
    --- eof ---

    but it has it's bad sides as you see, you may use trap for better
    debugging, but will not really works as you seems to want and I can't
    see an user case where you would want to do your example.

    Two comments:
    1) OP has not stated what his actual goal is, so we don't know anything
    about what the actual use case is. We may not ever hear back from
    OP, so we are left guessing. At least one poster on the thread has
    speculated that the goal is to capture the script name so that it
    can be used to log file entries and so on. If that is the goal,
    then there are many easier, better ways to do that.

    2) OP might be looking for $_, which generally holds the name of the
    last command executed (at least in bash; don't know about any other
    shells). This works in scripts. I sometimes use $_ to tell me
    whether or not a loop found a match (i.e., succeeded) or just ended
    because the loop ended. For example:

    for i in {list of files};do
    [ $i matches or whatever ] && break
    done
    [ $_ = break ] && echo "A match was found!" || echo "Loop exited without finding a match"

    --
    Watching ConservaLoons playing with statistics and facts is like watching a newborn play with a computer. Endlessly amusing, but totally unproductive.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Flash@21:1/5 to All on Thu Jun 30 16:50:25 2022
    XPost: alt.os.linux

    Il 30/06/22 14:04, Kenny McCormack ha scritto:
    OP has not stated what his actual goal is, so we don't know anything
    about what the actual use case is.

    $ tree dir | cat >> commands-output

    After a few days (in which I will have forgotten what I did exactly, in
    which I forgot the command I have in given...)

    $ cat commands-output
    dir
    ├── a
    ├── b
    └── c

    The output refers to what?
    Or rather, Output refers to which command?

    So at the first line I would have liked to have the reference command,
    and then everything else.

    Example

    $ cat commands-output
    REFERENCE COMMAND: $ tree dir
    dir
    ├── a
    ├── b
    └── c

    It can be done?

    -----BEGIN PGP MESSAGE-----

    hQGMAyHa6HqE1N3qAQwAivFYgm6kp+JCAOLdqF8HrJIEGO/bGqPpxzKGSlBURMAd tMmrZ5R66x6UhHCfV26+8+4R53A9a0cqCFZdM1imSorGA0A0vRLbz3AQATvR7Cf4 t5/MLKC4v8vHPKU8NA2ny+qaEBuLSPfiidPemqjRN8nLWUqUDh1sxevY8avYNp2L C8B1uTrDTGSG7Al7D7QacZP00WvfTI8JtgVeG+6aFwEJy+3SLMp/WqFdK18Vp/IL tgWDyIL1PMvcvA+GFlWCt9P/ULYBOfnqhfvGbkSFCBXwzRRvPxqy0BTARZm2hNRU 4fKHRj++msAW6BihU3kkTq95H/mfAn1RIwIqwX6bF7s/DprdqUtJ5Bx6H79dM6A4 hQyieK3oGdLuWtWxoxLu8akYuwJOJPy9vhqsPGIcTdT3109yg1CUfz+URZaF7HPo dXKk8fbHVlyhOZ2MrURWxCAjo1ngg3F58Xx1jf3rpXa3xCKHmDLnJUGcYoVd59wh LeeRHNrGoNr8ST0ipGsF0pYB/QLCDT/aXxIBC31We5eqNhGUgseh8ZqYKfWiU0sj E90+En2dADXGj5N/UkDr6xMoylDsO94a7FMxQZ07H5UVihYvGBaqtrnU4uMLOGs9 nle7QFLT5toWutoh44bZgeXkMsw1BAzijpg/FByro5pudb9qEum//KTDmGt+HaXd hjokxZtlFYPOOXmEUQO3XvMnayu+Vt4=
    =jI13
    -----END PGP MESSAGE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Flash@21:1/5 to All on Thu Jun 30 18:15:02 2022
    XPost: alt.os.linux

    Il 30/06/22 17:15, Lew Pitcher ha scritto:
    [...]
    $ rm typescript
    $ echo "tree" | script | cat -n
    1 Script iniziato, il file è typescript
    2 tree
    3 $ tree
    4 .
    5 ├── file1
    6 ├── file2
    7 └── typescript
    8
    9 0 directories, 3 files
    10 $ exit
    11 Script effettuato, il file è typescript
    $ test -f typescript && echo FILE FOUND
    FILE FOUND

    I want to send the output only to the "cat -n" command and not to the typescript file.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Flash on Thu Jun 30 15:15:31 2022
    XPost: alt.os.linux

    On Thu, 30 Jun 2022 16:50:25 +0200, Flash wrote:

    Il 30/06/22 14:04, Kenny McCormack ha scritto:
    OP has not stated what his actual goal is, so we don't know anything
    about what the actual use case is.

    $ tree dir | cat >> commands-output

    UUOC. Try
    tree dir >>commands-output
    instead


    After a few days (in which I will have forgotten what I did exactly, in
    which I forgot the command I have in given...)

    $ cat commands-output
    dir
    ├── a
    ├── b
    └── c

    The output refers to what?
    Or rather, Output refers to which command?

    So at the first line I would have liked to have the reference command,
    and then everything else.
    [snip]
    It can be done?

    Yes

    Try
    echo "tree dir" | script -a commands-output

    See script(1) ("man 1 script") for details


    -----BEGIN PGP MESSAGE-----
    [snip]
    -----END PGP MESSAGE-----
    And, why the encrypted message?

    HTH
    --
    Lew Pitcher
    "In Skills, We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kenny McCormack on Thu Jun 30 18:46:22 2022
    On 30.06.2022 14:04, Kenny McCormack wrote:

    2) OP might be looking for $_, which generally holds the name of the
    last command executed (at least in bash;

    Does it? - In ksh $_ keeps the last word of the previous command line,
    so 'break' or 'ls' will return the command name (incidentally), and
    'ls a b c' will return 'c'. - A quick bash test seems to indicate that
    it behaves like ksh in that respect.

    don't know about any other
    shells). This works in scripts. I sometimes use $_ to tell me
    whether or not a loop found a match (i.e., succeeded) or just ended
    because the loop ended. For example:

    for i in {list of files};do
    [ $i matches or whatever ] && break
    done
    [ $_ = break ] && echo "A match was found!" || echo "Loop exited without finding a match"


    Wouldn't that be enough...?

    for i in {list of files};do
    [ $i matches or whatever ] && break
    done && echo "A match was found!" || echo "Loop exited without finding a match"

    (works at least in ksh)

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J.O. Aho@21:1/5 to Flash on Thu Jun 30 19:41:55 2022
    XPost: alt.os.linux

    On 30/06/2022 16.50, Flash wrote:
    Il 30/06/22 14:04, Kenny McCormack ha scritto:
     OP has not stated what his actual goal is, so we don't know anything
        about what the actual use case is.

    $ tree dir | cat >> commands-output

    After a few days (in which I will have forgotten what I did exactly, in
    which I forgot the command I have in given...)


    so you want something like:

    --- runcmd ---
    #!/bin/bash
    CMD=$1
    echo "$CMD"
    eval $CMD
    --- eof ---

    runcmd "tree /dir" >> commands-output

    Maybe better to bookmark the webpage where you found the command line in
    the first place ;)

    history is a good command to get to know what you done before and you
    can add time stamp to it too

    https://www.cyberciti.biz/faq/unix-linux-bash-history-display-date-time/

    other alternatives:
    - use more describing file names so you know what the content is and
    how to regenerate it
    - Use bash audit and look in the logs.
    - make a script that does your work and can add a comment what input parameters was used


    --
    //Aho

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Flash on Thu Jun 30 19:44:54 2022
    XPost: alt.os.linux

    On Thu, 30 Jun 2022 18:15:02 +0200, Flash wrote:

    [snip]
    I want to send the output only to the "cat -n" command and not to the typescript file.

    Ahhh. But that's /not/ what you said in the post I replied to

    On Thu, 30 Jun 2022 16:50:25 +0200, Flash wrote:
    Flash> $ tree dir | cat >> commands-output
    Flash>
    Flash> After a few days (in which I will have forgotten what I did exactly, in
    Flash> which I forgot the command I have in given...)
    ...
    Flash>
    Flash> So at the first line I would have liked to have the reference command,
    Flash> and then everything else.

    You didn't say that you wanted the data line-numbered

    Anyway, I answered the question you asked.
    --
    Lew Pitcher
    "In Skills, We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Joe Beanfish@21:1/5 to Flash on Fri Jul 1 13:32:59 2022
    XPost: alt.os.linux

    On Thu, 30 Jun 2022 16:50:25 +0200, Flash wrote:

    Il 30/06/22 14:04, Kenny McCormack ha scritto:
    OP has not stated what his actual goal is, so we don't know anything
    about what the actual use case is.

    $ tree dir | cat >> commands-output

    After a few days (in which I will have forgotten what I did exactly, in
    which I forgot the command I have in given...)

    $ cat commands-output
    dir
    ├── a
    ├── b
    └── c

    The output refers to what?
    Or rather, Output refers to which command?

    So at the first line I would have liked to have the reference command,
    and then everything else.

    Example

    $ cat commands-output
    REFERENCE COMMAND: $ tree dir
    dir
    ├── a
    ├── b
    └── c

    It can be done?

    (set -x; tree a) >commands-output 2>&1

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Fri Jul 1 16:12:03 2022
    In article <t9kk0u$1v7i7$[email protected]>,
    Janis Papanagnou <[email protected]> wrote:
    On 30.06.2022 14:04, Kenny McCormack wrote:

    2) OP might be looking for $_, which generally holds the name of the
    last command executed (at least in bash;

    Does it? - In ksh $_ keeps the last word of the previous command line,
    so 'break' or 'ls' will return the command name (incidentally), and
    'ls a b c' will return 'c'. - A quick bash test seems to indicate that
    it behaves like ksh in that respect.

    What can I say? It works for me.

    Two comments:
    1) The test should actually be:
    [ X$_ = Xbreak ] && echo "It broke!"
    since otherwise you have a problem if $_ is empty (which I think it
    will be if you exit the loop "normally").

    2) My actual use case was a little more complicated than the "distilled
    down for posting to Usenet" version, and I was having problems
    getting it to work reliably doing it your way. In general, I'm not
    real comfortable with relying on "default" exit statuses (i.e.,
    those not explicitly set in code).

    But, anyway, it works for me. That's all that matters.

    --
    People who want to share their religious views with you
    almost never want you to share yours with them. -- Dave Barry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kenny McCormack on Fri Jul 1 18:55:41 2022
    On 01.07.2022 18:12, Kenny McCormack wrote:
    In article <t9kk0u$1v7i7$[email protected]>,
    Janis Papanagnou <[email protected]> wrote:
    On 30.06.2022 14:04, Kenny McCormack wrote:

    2) OP might be looking for $_, which generally holds the name of the >>> last command executed (at least in bash;

    Does it? - In ksh $_ keeps the last word of the previous command line,
    so 'break' or 'ls' will return the command name (incidentally), and
    'ls a b c' will return 'c'. - A quick bash test seems to indicate that
    it behaves like ksh in that respect.

    What can I say? It works for me.

    I just wanted to point out that as soon as you have a command with
    arguments it won't work any more; because the assumption that $_
    would contain the command name was obviously wrong.


    Two comments:
    1) The test should actually be:
    [ X$_ = Xbreak ] && echo "It broke!"
    since otherwise you have a problem if $_ is empty (which I think it
    will be if you exit the loop "normally").

    2) My actual use case was a little more complicated than the "distilled
    down for posting to Usenet" version, and I was having problems
    getting it to work reliably doing it your way.

    I considered that possibility, was sure you'll point that out if so.

    In general, I'm not
    real comfortable with relying on "default" exit statuses (i.e.,
    those not explicitly set in code).

    I see. Same here, but then I'd prefer setting and testing a flag
    explicitly.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Flash@21:1/5 to All on Fri Jul 1 18:48:44 2022
    XPost: alt.os.linux

    Il 01/07/22 15:32, Joe Beanfish ha scritto:
    (set -x; tree a) >commands-output 2>&1

    buona idea

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