I want to use the script
program to record my session any time that I am working within a certain directory, such as my ~/projects
directory.
So for example, I would cd
into ~/projects
, and script
would automatically start recording everything to a log in that directory. As soon as I leave ~/projects
, then script
would stop. Also, ~/projects
has many subdirectories; the log file needs to remain at the top level of the directory tree.
Any ideas on how this would be implemented? Even better, is there a method of implementing this that would also work with other programs easily?
export PROMPT_COMMAND="if [[ "$HOME/projects" == "$PWD" ]]; then unset PROMPT_COMMAND; script; fi
in your.profile
, but it won't automatically exit and will only trigger if you go into the top directory$PWD
, but its triggered manually. I was thinking about aliasing thecd
command in such a way that after changing to the desired directory it also performs this check and starts/endsscript
appropriately, but I wasn't sure if this might cause unforeseen problems. I'm not yet familiar with other ways to have actions performed in the background based on the$PWD
cd
command with a shell function that doescommand cd "$@"
and then checks$PWD
and startsscript
if the user is in the right directory. The problem is exitingscript
, because it fires up its own shell subprocess. The customcd
shell function could issue anexit
as soon as the user moves elsewhere, but since the current directory is local to each subprocess, the invocation ofcd
that takes the user out of the special directory would not be able to actually change his/her working directory.