Is it possible to findout if our command is sourced from a script or manually typed on command line?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












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.







share|improve this question



















  • 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















up vote
1
down vote

favorite
1












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.







share|improve this question



















  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.







share|improve this question











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.









share|improve this question










share|improve this question




share|improve this question









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

















  • 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











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





share|improve this answer




























    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.






    share|improve this answer





















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      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






























      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





      share|improve this answer

























        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





        share|improve this answer























          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





          share|improve this answer













          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






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 3 at 7:19









          rudimeier

          5,1111331




          5,1111331






















              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  share|improve this answer













                  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.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered May 3 at 7:19









                  skyking

                  1536




                  1536






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      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













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay