Re: script/history

2024-02-04 Thread Gareth Evans
On Mon 05/02/2024 at 00:45, Greg Wooledge  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



Re: script/history

2024-02-04 Thread Gareth Evans
(Re)posting the below as requested, and can confirm 

history -r 

seems to have the desired effect.
Thanks.

- Original message -
From: Will Mengarini 
To: Gareth Evans 
Cc: debian-user@lists.debian.org
Subject: Re: script/history
Date: Monday, 5 February 2024 01:02

* Gareth Evans  [24-02/04=Su 09:46 +]:
> 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.



Re: script/history

2024-02-04 Thread Will Mengarini
* Gareth Evans  [24-02/04=Su 09:46 +]:
> 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.



Re: script/history

2024-02-04 Thread Greg Wooledge
On Mon, Feb 05, 2024 at 12:28:38AM +, 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.



Re: script/history

2024-02-04 Thread Gareth Evans
On Sun 04/02/2024 at 19:45, David Wright  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



Re: script/history

2024-02-04 Thread Gareth Evans
On Sun 04/02/2024 at 17:33, Greg Wooledge  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



Re: script/history

2024-02-04 Thread Greg Wooledge
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.



Re: script/history

2024-02-04 Thread David Wright
On Sun 04 Feb 2024 at 16:01:29 (+), Gareth Evans wrote:
> On Sun 04/02/2024 at 13:24, Max Nikulin  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.



Re: script/history

2024-02-04 Thread Greg Wooledge
On Sun, Feb 04, 2024 at 04:01:29PM +, 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.



Re: script/history

2024-02-04 Thread Gareth Evans
On Sun 04/02/2024 at 13:24, Max Nikulin  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



Re: script/history

2024-02-04 Thread hw
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




Re: script/history

2024-02-04 Thread Max Nikulin

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.





Re: script/history

2024-02-04 Thread Greg Wooledge
On Sun, Feb 04, 2024 at 09:46:09AM +, 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
 and see where that takes you.



Re: script/history

2024-02-04 Thread Michael Grant
> $ 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


signature.asc
Description: PGP signature


Re: script/history

2024-02-04 Thread hw
On Sun, 2024-02-04 at 09: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?

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?



script/history

2024-02-04 Thread Gareth Evans
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