Execute code in a logout script if exiting from a particular directory?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have accounts on a number of different machines for testing a couple libraries I contribute to. Some of the machines are owned by others (like the GCC compile farm), and I'd like to keep them tidy.
I want to setup a .bash_logout
script that performs a make clean
upon exit
if I am in a particular directory. The directory requirement drops out of multiple SSH sessions. If I am building/testing in one session, I don't want a separate session logout cleaning the artifacts. That is, exit
from $HOME/libfoo
performs a clean; while exit
from $HOME
does not.
I understand the basics, but its not clear to me how robust this is:
# $HOME/.bash_logout
if [ "$PWD" = "$HOME/libfoo" ]; then
make clean 1>/dev/null 2>&1
fi
Are there any problems with using $PWD
during logout? Are there any other problems that I might have missed?
bash shell-script logout
add a comment |Â
up vote
3
down vote
favorite
I have accounts on a number of different machines for testing a couple libraries I contribute to. Some of the machines are owned by others (like the GCC compile farm), and I'd like to keep them tidy.
I want to setup a .bash_logout
script that performs a make clean
upon exit
if I am in a particular directory. The directory requirement drops out of multiple SSH sessions. If I am building/testing in one session, I don't want a separate session logout cleaning the artifacts. That is, exit
from $HOME/libfoo
performs a clean; while exit
from $HOME
does not.
I understand the basics, but its not clear to me how robust this is:
# $HOME/.bash_logout
if [ "$PWD" = "$HOME/libfoo" ]; then
make clean 1>/dev/null 2>&1
fi
Are there any problems with using $PWD
during logout? Are there any other problems that I might have missed?
bash shell-script logout
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have accounts on a number of different machines for testing a couple libraries I contribute to. Some of the machines are owned by others (like the GCC compile farm), and I'd like to keep them tidy.
I want to setup a .bash_logout
script that performs a make clean
upon exit
if I am in a particular directory. The directory requirement drops out of multiple SSH sessions. If I am building/testing in one session, I don't want a separate session logout cleaning the artifacts. That is, exit
from $HOME/libfoo
performs a clean; while exit
from $HOME
does not.
I understand the basics, but its not clear to me how robust this is:
# $HOME/.bash_logout
if [ "$PWD" = "$HOME/libfoo" ]; then
make clean 1>/dev/null 2>&1
fi
Are there any problems with using $PWD
during logout? Are there any other problems that I might have missed?
bash shell-script logout
I have accounts on a number of different machines for testing a couple libraries I contribute to. Some of the machines are owned by others (like the GCC compile farm), and I'd like to keep them tidy.
I want to setup a .bash_logout
script that performs a make clean
upon exit
if I am in a particular directory. The directory requirement drops out of multiple SSH sessions. If I am building/testing in one session, I don't want a separate session logout cleaning the artifacts. That is, exit
from $HOME/libfoo
performs a clean; while exit
from $HOME
does not.
I understand the basics, but its not clear to me how robust this is:
# $HOME/.bash_logout
if [ "$PWD" = "$HOME/libfoo" ]; then
make clean 1>/dev/null 2>&1
fi
Are there any problems with using $PWD
during logout? Are there any other problems that I might have missed?
bash shell-script logout
bash shell-script logout
asked Feb 18 '16 at 7:14
jww
1,51132158
1,51132158
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
Is your intent to execute the script only on log-out?
From man bash:
When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
Only on exit of a bash login shell.
If you intend to run an script on every shell close, use trap (see man bash):
trap /u1/myuser/on_exit_script.sh EXIT
Add it to .bashrc or some other that works for you.
Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:
if [ "$PWD" = "$HOME/libfoo" ]; then
Should work.
add a comment |Â
up vote
0
down vote
I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:
trap bye EXIT
New contributor
add a comment |Â
up vote
-1
down vote
I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Is your intent to execute the script only on log-out?
From man bash:
When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
Only on exit of a bash login shell.
If you intend to run an script on every shell close, use trap (see man bash):
trap /u1/myuser/on_exit_script.sh EXIT
Add it to .bashrc or some other that works for you.
Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:
if [ "$PWD" = "$HOME/libfoo" ]; then
Should work.
add a comment |Â
up vote
5
down vote
accepted
Is your intent to execute the script only on log-out?
From man bash:
When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
Only on exit of a bash login shell.
If you intend to run an script on every shell close, use trap (see man bash):
trap /u1/myuser/on_exit_script.sh EXIT
Add it to .bashrc or some other that works for you.
Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:
if [ "$PWD" = "$HOME/libfoo" ]; then
Should work.
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Is your intent to execute the script only on log-out?
From man bash:
When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
Only on exit of a bash login shell.
If you intend to run an script on every shell close, use trap (see man bash):
trap /u1/myuser/on_exit_script.sh EXIT
Add it to .bashrc or some other that works for you.
Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:
if [ "$PWD" = "$HOME/libfoo" ]; then
Should work.
Is your intent to execute the script only on log-out?
From man bash:
When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
Only on exit of a bash login shell.
If you intend to run an script on every shell close, use trap (see man bash):
trap /u1/myuser/on_exit_script.sh EXIT
Add it to .bashrc or some other that works for you.
Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:
if [ "$PWD" = "$HOME/libfoo" ]; then
Should work.
answered Feb 18 '16 at 10:54
user79743
add a comment |Â
add a comment |Â
up vote
0
down vote
I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:
trap bye EXIT
New contributor
add a comment |Â
up vote
0
down vote
I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:
trap bye EXIT
New contributor
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:
trap bye EXIT
New contributor
I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:
trap bye EXIT
New contributor
New contributor
answered 4 mins ago
volkerk
11
11
New contributor
New contributor
add a comment |Â
add a comment |Â
up vote
-1
down vote
I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory
add a comment |Â
up vote
-1
down vote
I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory
I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory
answered Feb 18 '16 at 10:19
Archelio
42
42
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f264019%2fexecute-code-in-a-logout-script-if-exiting-from-a-particular-directory%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password