How do I automatically record my bash sessions without running into a recursive loop?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do I automatically record my bash sessions without running into a recursive loop?
I want to run an experiment and record all my bash sessions. To do this I added the following command at the end of my .bashrc:
Code:
script ~/history/$(date +%F_%T.scr)
As you might imagine, because script starts a new bash session, .bashrc gets run again, which runs the command above again, which starts another bash session and so on.
How would I get around that? Here's my proposed solution, that I don't know how to implement: when I start a new bash session with script, I want to set a variable for that session that determines whether it's being recorded and prevent the script command from being executed in that session (using an if statement in .bashrc). I was thinking of using the -c argument of session to start a new bash session and set the variable manually. I can use the -c argument of the bash command to set a variable for that session, but then the session immediately exits, and I would like it to continue with that new variable being set.
That might have sounded confusing, here's what I'm thinking:
What are you trying to accomplish? If you're trying to save your history on a per session basis then you only need to modify the HISTFILE environment variable. That is what bash uses to save history.
In that case you need to check if the process script is already running, or you can check the SHLVL environment variable, or you can create a lock file somewhere (if it exists you do nothing, if not just create and start script - and at the end you will need to remove that lock file)
What are you trying to accomplish? If you're trying to save your history on a per session basis then you only need to modify the HISTFILE environment variable. That is what bash uses to save history.
If you're in a session but want to discard the history you can unset the variable.
Code:
unset HISTFILE
SAM
HISTFILE would work, except I wanted to capture command output as well. I guess I didn't really mention that. But I didn't know about HISTFILE, so thanks, I learned something new!
Quote:
Originally Posted by pan64
In that case you need to check if the process script is already running, or you can check the SHLVL environment variable, or you can create a lock file somewhere (if it exists you do nothing, if not just create and start script - and at the end you will need to remove that lock file)
The SHLVL variable does exactly what I needed (tell me whether a session is nested). Thank you!
There's been other posts on this and my comments are similar which is that one can save their history, but the tougher part is that you also want command output.
I personally stay away from the tricks and so forth. The more I try to abstract, the more I find that it takes away from my time which can be spent doing ... what I'm actually supposed to be doing. Therefore I go the tried and true. My terminal recall windows have unlimited scrollback/memory/whatever you call it. I do something which I need to record or remember, I select all and paste it into a TXT file and also edit that file to add comments. Because some stuff is also notes like "I tried this ... but it didn't work ... so I had to reverse it ... and that's what all this garbage is ..."
Further, I do embedded systems and as a result I have consoles, some of them limited in features or whatever. I'm also installing tons and tweaking when I first get going on a system and this is the exact time I need to recall exactly what I did. The point there is I can't afford to install my bang-zoom-automagic script recall; and then only to find that things like busybox acts differently or that certain commands I use in that script aren't on that system, or the fact that I rely on a particular directory structure to save my stuff; etc. Rather the tried and true copy/paste/edit notes method does work and works well. It's really just the awareness that, that activity is important at the present juncture; so that terminal where I ssh to the target, I can copy all and paste it out. Or the terminal where I have minicom open as my serial console, same result.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.