Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #266937 > unrolled thread

script/history

Started by"Gareth Evans" <donotspam@fastmail.fm>
First post2024-02-04 10:50 +0100
Last post2024-02-05 02:20 +0100
Articles 16 — 7 participants

Back to article view | Back to linux.debian.user


Contents

  script/history "Gareth Evans" <donotspam@fastmail.fm> - 2024-02-04 10:50 +0100
    Re: script/history hw <hw@adminart.net> - 2024-02-04 12:20 +0100
    Re: script/history Michael Grant <mgrant@grant.org> - 2024-02-04 12:30 +0100
      Re: script/history "Gareth Evans" <donotspam@fastmail.fm> - 2024-02-04 14:20 +0100
      Re: script/history hw <hw@adminart.net> - 2024-02-04 14:30 +0100
    Re: script/history Greg Wooledge <greg@wooledge.org> - 2024-02-04 14:30 +0100
    Re: script/history Max Nikulin <manikulin@gmail.com> - 2024-02-04 14:30 +0100
      Re: script/history "Gareth Evans" <donotspam@fastmail.fm> - 2024-02-04 17:10 +0100
        Re: script/history Greg Wooledge <greg@wooledge.org> - 2024-02-04 18:40 +0100
          Re: script/history "Gareth Evans" <donotspam@fastmail.fm> - 2024-02-05 01:30 +0100
            Re: script/history Greg Wooledge <greg@wooledge.org> - 2024-02-05 01:50 +0100
              Re: script/history "Gareth Evans" <donotspam@fastmail.fm> - 2024-02-05 02:30 +0100
        Re: script/history David Wright <deblis@lionunicorn.co.uk> - 2024-02-04 20:50 +0100
          Re: script/history Greg Wooledge <greg@wooledge.org> - 2024-02-04 21:40 +0100
          Re: script/history "Gareth Evans" <donotspam@fastmail.fm> - 2024-02-05 01:40 +0100
    Re: script/history Will Mengarini <seldon@eskimo.com> - 2024-02-05 02:20 +0100

#266937 — script/history

From"Gareth Evans" <donotspam@fastmail.fm>
Date2024-02-04 10:50 +0100
Subjectscript/history
Message-ID<I3Hip-7Nv9-1@gated-at.bofh.it>
Re the script command, does anyone know of a way to make commands run during a script session appear in bash history too?  

$ script foo.txt
Script started, output log file is 'foo.txt'.
$ date
Sun  4 Feb 09:44:00 GMT 2024
$ exit
exit
Script done.
$ history|tail -n2
30797  2024-02-04 09:43:57  script foo.txt
30798  2024-02-04 09:44:21  history|tail -n2

I did try to search on this but just got lots of "bash history" and "history in bash script" references.

man script says 

"SEE ALSO
       csh(1) (for the history mechanism)"

but

$ man csh
No manual entry for csh

Thanks
Gareth

[toc] | [next] | [standalone]


#266940

Fromhw <hw@adminart.net>
Date2024-02-04 12:20 +0100
Message-ID<I3IHv-7ODN-19@gated-at.bofh.it>
In reply to#266937
On Sun, 2024-02-04 at 09:46 +0000, Gareth Evans wrote:
> Re the script command, does anyone know of a way to make commands
> run during a script session appear in bash history too?

Maybe this:
https://serverfault.com/questions/16204/how-to-make-bash-scripts-print-out-every-command-before-it-executes

It seems awkward to have scripts being put into the history.  You
could do something like

cat script.sh >> ~/.bash_history

maybe.  How would that be useful?

[toc] | [prev] | [next] | [standalone]


#266941

FromMichael Grant <mgrant@grant.org>
Date2024-02-04 12:30 +0100
Message-ID<I3IRe-7OGZ-9@gated-at.bofh.it>
In reply to#266937

[Multipart message — attachments visible in raw view] — view raw

> $ script foo.txt
> Script started, output log file is 'foo.txt'.
> $ date
> Sun  4 Feb 09:44:00 GMT 2024
> $ exit
> exit
> Script done.
> $ history|tail -n2
> 30797  2024-02-04 09:43:57  script foo.txt
> 30798  2024-02-04 09:44:21  history|tail -n2
> 
> I did try to search on this but just got lots of "bash history" and "history in
> bash script" references.

So this might surprise you but the commands are actually in the
history list!  But not in the current shell.

What happens is this:

You start 'script foo.txt' and this starts a sub bash shell on a
different pseudo tty.  You run some commands, it appends each command
to the history of this sub-shell's history.

You then exit your script.  Those commands you ran are at the bottom
of .bash_history (try to cat that file out after you exit script and
you should see them).

But those commands are not sucked into the history of your current
shell.  Then, you log out (or exit) your current shell and the history
of that shell overwrites the history of the previous one.

If all you want to do is save off the commands after you exit your
script session, then simply move or copy .bash_history out of the way
before it gets overwritten.

You might consider setting $HISTFILE to some other location other than
.bash_history.

Michael Grant

[toc] | [prev] | [next] | [standalone]


#266945

From"Gareth Evans" <donotspam@fastmail.fm>
Date2024-02-04 14:20 +0100
Message-ID<I3KzD-7PUR-19@gated-at.bofh.it>
In reply to#266941
On Sun 04/02/2024 at 11:28, Michael Grant <mgrant@grant.org> wrote:
>> $ script foo.txt
>> Script started, output log file is 'foo.txt'.
>> $ date
>> Sun  4 Feb 09:44:00 GMT 2024
>> $ exit
>> exit
>> Script done.
>> $ history|tail -n2
>> 30797  2024-02-04 09:43:57  script foo.txt
>> 30798  2024-02-04 09:44:21  history|tail -n2
>> 
>> I did try to search on this but just got lots of "bash history" and "history in
>> bash script" references.
>
> So this might surprise you but the commands are actually in the
> history list!  But not in the current shell.

So they are :)

Thanks
G

[toc] | [prev] | [next] | [standalone]


#266947

Fromhw <hw@adminart.net>
Date2024-02-04 14:30 +0100
Message-ID<I3KJk-7PXW-5@gated-at.bofh.it>
In reply to#266941
Oh you're right, I entirely overlooked the usage of 'script' and
didn't understand the question right, sorry.

On Sun, 2024-02-04 at 06:28 -0500, Michael Grant wrote:
> > $ script foo.txt
> > Script started, output log file is 'foo.txt'.
> > $ date
> > Sun  4 Feb 09:44:00 GMT 2024
> > $ exit
> > exit
> > Script done.
> > $ history|tail -n2
> > 30797  2024-02-04 09:43:57  script foo.txt
> > 30798  2024-02-04 09:44:21  history|tail -n2
> > 
> > I did try to search on this but just got lots of "bash history" and "history in
> > bash script" references.
> 
> So this might surprise you but the commands are actually in the
> history list!  But not in the current shell.
> 
> What happens is this:
> 
> You start 'script foo.txt' and this starts a sub bash shell on a
> different pseudo tty.  You run some commands, it appends each command
> to the history of this sub-shell's history.
> 
> You then exit your script.  Those commands you ran are at the bottom
> of .bash_history (try to cat that file out after you exit script and
> you should see them).
> 
> But those commands are not sucked into the history of your current
> shell.  Then, you log out (or exit) your current shell and the history
> of that shell overwrites the history of the previous one.
> 
> If all you want to do is save off the commands after you exit your
> script session, then simply move or copy .bash_history out of the way
> before it gets overwritten.
> 
> You might consider setting $HISTFILE to some other location other than
> .bash_history.
> 
> Michael Grant

[toc] | [prev] | [next] | [standalone]


#266946

FromGreg Wooledge <greg@wooledge.org>
Date2024-02-04 14:30 +0100
Message-ID<I3KJk-7PXW-13@gated-at.bofh.it>
In reply to#266937
On Sun, Feb 04, 2024 at 09:46:09AM +0000, Gareth Evans wrote:
> man script says 
> 
> "SEE ALSO
>        csh(1) (for the history mechanism)"
> 
> but
> 
> $ man csh
> No manual entry for csh

I'm so glad that we're entering an era where it's normal *not* to have
csh installed and used.  That shell really was a disaster.

Anyway, if you want to read man pages for packages that are not installed
on your system, the World Wide Web is a good source.

Specifically, you could start with
<https://manpages.debian.org/csh> and see where that takes you.

[toc] | [prev] | [next] | [standalone]


#266948

FromMax Nikulin <manikulin@gmail.com>
Date2024-02-04 14:30 +0100
Message-ID<I3KJk-7PXW-11@gated-at.bofh.it>
In reply to#266937
On 04/02/2024 16:46, Gareth Evans wrote:
> Re the script command, does anyone know of a way to make commands run during a script session appear in bash history too?
[...]
> man script says
> 
> "SEE ALSO
>         csh(1) (for the history mechanism)"
> 
> but
> 
> $ man csh
> No manual entry for csh

     echo $SHELL

If you really use BASH then install bash-doc, it contains manual in 
various formats, including texinfo (info "bash") or read it online
https://www.gnu.org/software/bash/manual/
it describes some options related to history handling Actually "man 
bash" is something rather close, but it does not allow to save links to 
specific sections.

[toc] | [prev] | [next] | [standalone]


#266951

From"Gareth Evans" <donotspam@fastmail.fm>
Date2024-02-04 17:10 +0100
Message-ID<I3Ne9-7Rxx-1@gated-at.bofh.it>
In reply to#266948
On Sun 04/02/2024 at 13:24, Max Nikulin <manikulin@gmail.com> wrote:
> On 04/02/2024 16:46, Gareth Evans wrote:
>> Re the script command, does anyone know of a way to make commands run during a script session appear in bash history too?
> [...]
>> man script says
>> 
>> "SEE ALSO
>>         csh(1) (for the history mechanism)"
>> 
>> but
>> 
>> $ man csh
>> No manual entry for csh
>
>      echo $SHELL
>
> If you really use BASH then install bash-doc, it contains manual in 
> various formats, including texinfo (info "bash") or read it online
> https://www.gnu.org/software/bash/manual/
> it describes some options related to history handling Actually "man 
> bash" is something rather close, but it does not allow to save links to 
> specific sections.

Hi Max,

Thanks for links and suggestions.

It seemed to me initially (as I should perhaps have stated) that man script was suggesting that csh was a component or depedency (of script), which seemed to be contradicted by it not being installed.  On reflection, possibly, it's just a way of doing things in script.  The reference in man script now seems to imply the latter.  

The fact that script scripts bash on my machine made me not think about the fact that it probably scripts whatever shell it's running from, and was doubtless written with one in mind/as inspiration - csh might make a certain sense in that respect.  

script runs in bash if run from csh run from bash, but I suppose csh in that chain of shells isn't a proper [do I mean login?] shell.  (Got that? :p)

The function of the "a" option in History Substitution in man csh seems different in the bash version.(under "Word Designators" in man bash/gnu online manual)

It'll be interesting to test when I can make sense of it :p

Anyway, my orginal question has been answered.

Thanks all.
Gareth

[toc] | [prev] | [next] | [standalone]


#266956

FromGreg Wooledge <greg@wooledge.org>
Date2024-02-04 18:40 +0100
Message-ID<I3ODf-7SgL-13@gated-at.bofh.it>
In reply to#266951
On Sun, Feb 04, 2024 at 04:01:29PM +0000, Gareth Evans wrote:
> It seemed to me initially (as I should perhaps have stated) that man script was suggesting that csh was a component or depedency (of script), which seemed to be contradicted by it not being installed.  On reflection, possibly, it's just a way of doing things in script.  The reference in man script now seems to imply the latter.  
> 
> The fact that script scripts bash on my machine made me not think about the fact that it probably scripts whatever shell it's running from, and was doubtless written with one in mind/as inspiration - csh might make a certain sense in that respect.  
> 
> script runs in bash if run from csh run from bash, but I suppose csh in that chain of shells isn't a proper [do I mean login?] shell.  (Got that? :p)

You're so confused that I can't even figure out where to start!  So
let me start here:

The script(1) utility exists to RECORD a session so that you can play
it back later and see what happened.

One recommended use of this tool is to record the session in which you
do a dist-upgrade, say, from Debian 11 to Debian 12.  Then if anything
goes wrong, you can look back through the session's logfile and see
what happened, and try to fix it.

The script(1) utility has NOTHING to do with running ordinary shell
scripts.

[toc] | [prev] | [next] | [standalone]


#266964

From"Gareth Evans" <donotspam@fastmail.fm>
Date2024-02-05 01:30 +0100
Message-ID<I3V21-7We1-1@gated-at.bofh.it>
In reply to#266956
On Sun 04/02/2024 at 17:33, Greg Wooledge <greg@wooledge.org> wrote:
...
> The script(1) utility has NOTHING to do with running ordinary shell
> scripts.

I understand that.

I was trying to view the history of commands run during a script session.

user@qwerty:~$ script foo
Script started, output log file is 'foo'.
user@qwerty:~$ date
Mon  5 Feb 00:21:16 GMT 2024
user@qwerty:~$ exit
exit
Script done.
user@qwerty:~$ history |tail -n3
30914  2024-02-04 23:44:24  man script
30915  2024-02-05 00:21:15  script foo
30916  2024-02-05 00:21:25  history |tail -n3 # NB "date" is missing

Michael Grant pointed out (among other things) that the history is available, but not in the terminal in which script has just exited.

Meanwhile, I read man script and wondered if csh, or even just csh's approach to history manipulation, was somehow involved.

I was then a little surprised to see 

user@qwerty:~$ csh             # bash prompt
% script foo                   # csh prompt
Script started, output log file is 'foo'.
user@qwerty:~$ date            # bash prompt in script in csh in bash
Sun  4 Feb 23:25:14 GMT 2024
user@qwerty:~$ exit
exit
Script done.
% exit
% exit
user@qwerty:~$ 

although on a "native" csh system, I would expect script to "present" csh, not bash.  I 

I am left with the impression that script may impose csh's history substitution/manipulation approach/syntax (for what differences there may be) regardless of which shell it's running from, and that that is the reason for the reference to csh for the "history mechanism" in man script.

Thanks
Gareth

[toc] | [prev] | [next] | [standalone]


#266966

FromGreg Wooledge <greg@wooledge.org>
Date2024-02-05 01:50 +0100
Message-ID<I3Vln-7Wk2-1@gated-at.bofh.it>
In reply to#266964
On Mon, Feb 05, 2024 at 12:28:38AM +0000, Gareth Evans wrote:
> I was trying to view the history of commands run during a script session.
> 
> user@qwerty:~$ script foo
> Script started, output log file is 'foo'.
> user@qwerty:~$ date
> Mon  5 Feb 00:21:16 GMT 2024
> user@qwerty:~$ exit
> exit
> Script done.
> user@qwerty:~$ history |tail -n3
> 30914  2024-02-04 23:44:24  man script
> 30915  2024-02-05 00:21:15  script foo
> 30916  2024-02-05 00:21:25  history |tail -n3 # NB "date" is missing
> 
> Michael Grant pointed out (among other things) that the history is available, but not in the terminal in which script has just exited.

OK.  Well, start by thinking about what happens if you're in a bash
shell, and you type "bash".  You get a second bash instance, which is a
child process of the original shell.  The original shell goes to sleep,
and waits for the child to exit.

When the child process starts up, it reads the .bash_history file (unless
you changed the variable that names the file), thereby loading the shell
history from disk into memory.  Then you run whatever commands you run,
and those are appended to the in-memory shell history.  Then you exit,
at which point bash rewrites the .bash_history file.

After exiting from the child shell, you're back in the original shell.
The original shell's in-memory history did not change during the child's
lifetime.  The only change to the in-memory history is the addition of
a "bash" command.  The .bash_history file has been changed, but the
running shell doesn't read that to get the new contents in memory.

If/when you exit from the original shell, the .bash_history file will
be rewritten again, using the in-memory history of the original bash
instance.  This does *not* include any changes that were written by
the child process.  Those changes may simply be lost.  It depends on
which history options you've chosen, and how many new commands were
appended to the original shell's in-memory history before exiting.

Once you understand all of this, then it should be obvious how "script"
works.  It runs a new instance of "bash".  As far as history goes, it
works exactly like the above.  The only difference is you've also got
a file containing a recording of the terminal session.

If you're one of these "I want every command I ever run to be in my
shell history, retained forever, and I don't care how much space it
takes" people, then there are web pages out there that can help you.
I don't follow that philosophy myself, so I can't give you specific
variables and settings.  But Google can.

[toc] | [prev] | [next] | [standalone]


#266969

From"Gareth Evans" <donotspam@fastmail.fm>
Date2024-02-05 02:30 +0100
Message-ID<I3VY6-7WLZ-1@gated-at.bofh.it>
In reply to#266966
On Mon 05/02/2024 at 00:45, Greg Wooledge <greg@wooledge.org> wrote:
...
> If you're one of these "I want every command I ever run to be in my
> shell history, retained forever, and I don't care how much space it
> takes" people, then there are web pages out there that can help you.
> I don't follow that philosophy myself, so I can't give you specific
> variables and settings.  But Google can.

I find a long history useful, and have already googled for such, though I should perhaps rotate it at some point :)

Thanks for the detailed explanation.
G

[toc] | [prev] | [next] | [standalone]


#266960

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2024-02-04 20:50 +0100
Message-ID<I3QF3-7Tuv-5@gated-at.bofh.it>
In reply to#266951
On Sun 04 Feb 2024 at 16:01:29 (+0000), Gareth Evans wrote:
> On Sun 04/02/2024 at 13:24, Max Nikulin <manikulin@gmail.com> wrote:
> > On 04/02/2024 16:46, Gareth Evans wrote:
> >> Re the script command, does anyone know of a way [ … ]
> > [...]
> >> man script says
> >> 
> >> "SEE ALSO
> >>         csh(1) (for the history mechanism)"

My take on this is that the man page was originally written for
BSD, which lies on the csh side of the "great divide" rather
than the sh/bash side.

SCRIPT(1)             User Commands                  SCRIPT(1)
[ … ]
HISTORY
       The script command appeared in 3.0BSD.

I have no idea why "the history mechanism" is even mentioned
in the man page for script.

> The function of the "a" option in History Substitution in man csh seems different in the bash version.(under "Word Designators" in man bash/gnu online manual)

According to this man page for csh (but includes tcsh):

  https://linux.die.net/man/1/csh

the "a" that modifies modifiers is a "[feature] of tcsh not found
in most csh(1) implementations (specifically, the 4.4BSD csh)". It
appears that bash supports it syntactically, but not its semantics.
I'm not sure why you mentioned this shell detail specifically.

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#266962

FromGreg Wooledge <greg@wooledge.org>
Date2024-02-04 21:40 +0100
Message-ID<I3Rrr-7TZY-1@gated-at.bofh.it>
In reply to#266960
On Sun, Feb 04, 2024 at 01:45:27PM -0600, David Wright wrote:
> SCRIPT(1)             User Commands                  SCRIPT(1)
> [ … ]
> HISTORY
>        The script command appeared in 3.0BSD.
> 
> I have no idea why "the history mechanism" is even mentioned
> in the man page for script.

It appears in the FreeBSD man page as well, but not the OpenBSD man
page.

https://man.freebsd.org/cgi/man.cgi?script
https://man.openbsd.org/script

I don't understand why that parenthetical comment is in the FreeBSD
man page either, but it was clearly copied over to the util-linux
man page without modification.

Another thing I don't understand is what the OP of this thread was
trying to do in the first place.

[toc] | [prev] | [next] | [standalone]


#266965

From"Gareth Evans" <donotspam@fastmail.fm>
Date2024-02-05 01:40 +0100
Message-ID<I3VbH-7WgY-3@gated-at.bofh.it>
In reply to#266960
On Sun 04/02/2024 at 19:45, David Wright <deblis@lionunicorn.co.uk> wrote:
...
> According to this man page for csh (but includes tcsh):
>
>   https://linux.die.net/man/1/csh
>
> the "a" that modifies modifiers is a "[feature] of tcsh not found
> in most csh(1) implementations (specifically, the 4.4BSD csh)". It
> appears that bash supports it syntactically, but not its semantics.
> I'm not sure why you mentioned this shell detail specifically.

I was just highlighting a way to see if behaviour differs between bash "proper" and script running in bash.

cf.

"a (+)
Apply the following modifier as many times as possible to a single word ..."
https://linux.die.net/man/1/csh

"a    
Cause changes to be applied over the entire event line ..."
https://www.gnu.org/software/bash/manual/bash.html#Word-Designators

Thanks
Gareth

[toc] | [prev] | [next] | [standalone]


#266968

FromWill Mengarini <seldon@eskimo.com>
Date2024-02-05 02:20 +0100
Message-ID<I3VOp-7WJ2-3@gated-at.bofh.it>
In reply to#266937
* Gareth Evans <donotspam@fastmail.fm> [24-02/04=Su 09:46 +0000]:
> Re the script command, does anyone know of a way to make
> commands run during a script session appear in bash history too?

You want the 'history -r' command, "explained" by `help history`.

After you end the script, you're back in the bash instance you ran
'script' from.  When you ended the script & therefore the bash being run
in that script, that bash wrote its commands to ~/.bash_history; but
the bash you're running, which is the one you ran script from, hasn't
read that ~/.bash_history.  Running 'history -r' will cause it to do so.

BTW I've got some kind of email configuration problem that usually
prevents me from posting to debian-user, even though I can usually
email individual users.  Gareth, if you get this, and it's the correct
solution to your original problem, perhaps you could post it to the
list; this email is CCd to debian-user, but I expect the CC to bounce.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web