Is it possible to findout if our command is sourced from a script or manually typed on command line?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
We have an implementation to set environment for a particular tool with our custom command.
Eg: custom_command tool_name
This command logs the invokation time, user and tool_name in a log file.
And with these logs, we can findout when was a particular tool last used.
Now, users have this habit of putting the command in their login profile.
So, is it possible to findout if a comamnd is invoked by manually typing it on command line or is sourced from another script and if it is, what was the script name?
I have tried multiple ways. All I can findout is the parent script name i.e., the shell.
My understanding is that it is not possible. But I am just trying my luck.
shell-script shell fork subshell
add a comment |Â
up vote
1
down vote
favorite
We have an implementation to set environment for a particular tool with our custom command.
Eg: custom_command tool_name
This command logs the invokation time, user and tool_name in a log file.
And with these logs, we can findout when was a particular tool last used.
Now, users have this habit of putting the command in their login profile.
So, is it possible to findout if a comamnd is invoked by manually typing it on command line or is sourced from another script and if it is, what was the script name?
I have tried multiple ways. All I can findout is the parent script name i.e., the shell.
My understanding is that it is not possible. But I am just trying my luck.
shell-script shell fork subshell
Related: unix.stackexchange.com/questions/26676/â¦
â Kusalananda
May 3 at 6:37
Note that the related question is just that (it's not by no means the same).
â skyking
May 3 at 6:39
@Kusalananda I have checked if there is any change in shopt options when sourcing with script vs directly running the script on command line, there are no changes at any options. Any other I can try like shopt?
â Jeevan Patnaik
May 3 at 7:03
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
We have an implementation to set environment for a particular tool with our custom command.
Eg: custom_command tool_name
This command logs the invokation time, user and tool_name in a log file.
And with these logs, we can findout when was a particular tool last used.
Now, users have this habit of putting the command in their login profile.
So, is it possible to findout if a comamnd is invoked by manually typing it on command line or is sourced from another script and if it is, what was the script name?
I have tried multiple ways. All I can findout is the parent script name i.e., the shell.
My understanding is that it is not possible. But I am just trying my luck.
shell-script shell fork subshell
We have an implementation to set environment for a particular tool with our custom command.
Eg: custom_command tool_name
This command logs the invokation time, user and tool_name in a log file.
And with these logs, we can findout when was a particular tool last used.
Now, users have this habit of putting the command in their login profile.
So, is it possible to findout if a comamnd is invoked by manually typing it on command line or is sourced from another script and if it is, what was the script name?
I have tried multiple ways. All I can findout is the parent script name i.e., the shell.
My understanding is that it is not possible. But I am just trying my luck.
shell-script shell fork subshell
asked May 3 at 6:29
Jeevan Patnaik
1952518
1952518
Related: unix.stackexchange.com/questions/26676/â¦
â Kusalananda
May 3 at 6:37
Note that the related question is just that (it's not by no means the same).
â skyking
May 3 at 6:39
@Kusalananda I have checked if there is any change in shopt options when sourcing with script vs directly running the script on command line, there are no changes at any options. Any other I can try like shopt?
â Jeevan Patnaik
May 3 at 7:03
add a comment |Â
Related: unix.stackexchange.com/questions/26676/â¦
â Kusalananda
May 3 at 6:37
Note that the related question is just that (it's not by no means the same).
â skyking
May 3 at 6:39
@Kusalananda I have checked if there is any change in shopt options when sourcing with script vs directly running the script on command line, there are no changes at any options. Any other I can try like shopt?
â Jeevan Patnaik
May 3 at 7:03
Related: unix.stackexchange.com/questions/26676/â¦
â Kusalananda
May 3 at 6:37
Related: unix.stackexchange.com/questions/26676/â¦
â Kusalananda
May 3 at 6:37
Note that the related question is just that (it's not by no means the same).
â skyking
May 3 at 6:39
Note that the related question is just that (it's not by no means the same).
â skyking
May 3 at 6:39
@Kusalananda I have checked if there is any change in shopt options when sourcing with script vs directly running the script on command line, there are no changes at any options. Any other I can try like shopt?
â Jeevan Patnaik
May 3 at 7:03
@Kusalananda I have checked if there is any change in shopt options when sourcing with script vs directly running the script on command line, there are no changes at any options. Any other I can try like shopt?
â Jeevan Patnaik
May 3 at 7:03
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
In bash
scripts I do it like this usually:
#!/bin/bash
if test "$0" != "$BASH_SOURCE"; then
SOURCED=1
MY_NAME=$BASH_SOURCE
else
SOURCED=0
MY_NAME=$0
fi
# do something here
if test "$SOURCED" = "1"; then
# use return instead of exit
return
fi
# do something here only when not sourced
exit
add a comment |Â
up vote
0
down vote
I don't think it's entirely possible.
Granted you can find out what the parent process is and what it's command line parameters are, but at the end of the day when running an interactive shell the parent process of the programs executed from startup script is the same as the programs executed interactively.
If it only where execution from the .profile
you could detect that by the fact that /etc/bashrc
is sourced after that (in that you could put a command to stop ignoring executions from this shell).
Another approach could be to check the starting time of the parent process. Often interactive execution of a command would be done a while after the shell has started while the init scripts will execute shortly after the shell has started.
A more elaborate idea is to replace /bin/bash
with a wrapper that controls the sourcing of the startup files in a more customized manner (the idea is similar to the first idea, but to get that you get your command run after .bashrc
). I don't know if this is a good idea, but at least you should be really careful if doing it.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
In bash
scripts I do it like this usually:
#!/bin/bash
if test "$0" != "$BASH_SOURCE"; then
SOURCED=1
MY_NAME=$BASH_SOURCE
else
SOURCED=0
MY_NAME=$0
fi
# do something here
if test "$SOURCED" = "1"; then
# use return instead of exit
return
fi
# do something here only when not sourced
exit
add a comment |Â
up vote
0
down vote
In bash
scripts I do it like this usually:
#!/bin/bash
if test "$0" != "$BASH_SOURCE"; then
SOURCED=1
MY_NAME=$BASH_SOURCE
else
SOURCED=0
MY_NAME=$0
fi
# do something here
if test "$SOURCED" = "1"; then
# use return instead of exit
return
fi
# do something here only when not sourced
exit
add a comment |Â
up vote
0
down vote
up vote
0
down vote
In bash
scripts I do it like this usually:
#!/bin/bash
if test "$0" != "$BASH_SOURCE"; then
SOURCED=1
MY_NAME=$BASH_SOURCE
else
SOURCED=0
MY_NAME=$0
fi
# do something here
if test "$SOURCED" = "1"; then
# use return instead of exit
return
fi
# do something here only when not sourced
exit
In bash
scripts I do it like this usually:
#!/bin/bash
if test "$0" != "$BASH_SOURCE"; then
SOURCED=1
MY_NAME=$BASH_SOURCE
else
SOURCED=0
MY_NAME=$0
fi
# do something here
if test "$SOURCED" = "1"; then
# use return instead of exit
return
fi
# do something here only when not sourced
exit
answered May 3 at 7:19
rudimeier
5,1111331
5,1111331
add a comment |Â
add a comment |Â
up vote
0
down vote
I don't think it's entirely possible.
Granted you can find out what the parent process is and what it's command line parameters are, but at the end of the day when running an interactive shell the parent process of the programs executed from startup script is the same as the programs executed interactively.
If it only where execution from the .profile
you could detect that by the fact that /etc/bashrc
is sourced after that (in that you could put a command to stop ignoring executions from this shell).
Another approach could be to check the starting time of the parent process. Often interactive execution of a command would be done a while after the shell has started while the init scripts will execute shortly after the shell has started.
A more elaborate idea is to replace /bin/bash
with a wrapper that controls the sourcing of the startup files in a more customized manner (the idea is similar to the first idea, but to get that you get your command run after .bashrc
). I don't know if this is a good idea, but at least you should be really careful if doing it.
add a comment |Â
up vote
0
down vote
I don't think it's entirely possible.
Granted you can find out what the parent process is and what it's command line parameters are, but at the end of the day when running an interactive shell the parent process of the programs executed from startup script is the same as the programs executed interactively.
If it only where execution from the .profile
you could detect that by the fact that /etc/bashrc
is sourced after that (in that you could put a command to stop ignoring executions from this shell).
Another approach could be to check the starting time of the parent process. Often interactive execution of a command would be done a while after the shell has started while the init scripts will execute shortly after the shell has started.
A more elaborate idea is to replace /bin/bash
with a wrapper that controls the sourcing of the startup files in a more customized manner (the idea is similar to the first idea, but to get that you get your command run after .bashrc
). I don't know if this is a good idea, but at least you should be really careful if doing it.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I don't think it's entirely possible.
Granted you can find out what the parent process is and what it's command line parameters are, but at the end of the day when running an interactive shell the parent process of the programs executed from startup script is the same as the programs executed interactively.
If it only where execution from the .profile
you could detect that by the fact that /etc/bashrc
is sourced after that (in that you could put a command to stop ignoring executions from this shell).
Another approach could be to check the starting time of the parent process. Often interactive execution of a command would be done a while after the shell has started while the init scripts will execute shortly after the shell has started.
A more elaborate idea is to replace /bin/bash
with a wrapper that controls the sourcing of the startup files in a more customized manner (the idea is similar to the first idea, but to get that you get your command run after .bashrc
). I don't know if this is a good idea, but at least you should be really careful if doing it.
I don't think it's entirely possible.
Granted you can find out what the parent process is and what it's command line parameters are, but at the end of the day when running an interactive shell the parent process of the programs executed from startup script is the same as the programs executed interactively.
If it only where execution from the .profile
you could detect that by the fact that /etc/bashrc
is sourced after that (in that you could put a command to stop ignoring executions from this shell).
Another approach could be to check the starting time of the parent process. Often interactive execution of a command would be done a while after the shell has started while the init scripts will execute shortly after the shell has started.
A more elaborate idea is to replace /bin/bash
with a wrapper that controls the sourcing of the startup files in a more customized manner (the idea is similar to the first idea, but to get that you get your command run after .bashrc
). I don't know if this is a good idea, but at least you should be really careful if doing it.
answered May 3 at 7:19
skyking
1536
1536
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%2f441464%2fis-it-possible-to-findout-if-our-command-is-sourced-from-a-script-or-manually-ty%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
Related: unix.stackexchange.com/questions/26676/â¦
â Kusalananda
May 3 at 6:37
Note that the related question is just that (it's not by no means the same).
â skyking
May 3 at 6:39
@Kusalananda I have checked if there is any change in shopt options when sourcing with script vs directly running the script on command line, there are no changes at any options. Any other I can try like shopt?
â Jeevan Patnaik
May 3 at 7:03