Find the answer to your Linux question:
Results 1 to 9 of 9
Guys,I would like to know if there is any way to detect a command if it going to be executed in the shell? Eg: Cmd: sudo apt-get clean I want ...
  1. #1
    Just Joined! kapmsd's Avatar
    Join Date
    Sep 2009
    Posts
    17

    Detection of a command in shell

    Guys,I would like to know if there is any way to detect a command if it going to be executed in the shell?
    Eg:
    Cmd: sudo apt-get clean
    I want a C program to be called before this cmd is executed.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined! sixdrift's Avatar
    Join Date
    Jan 2007
    Location
    In and around and about Cary, NC
    Posts
    44
    I think the implication here is to alias the apt-get command so that it kicks off a script that runs any number of commands before doing the actual intended operation. Or you could alias sudo I guess and then catch any command run with sudo.

    Or are you asking is there a way to detect any arbitrary command, even without a "sudo" in front?

  4. #4
    Just Joined! kapmsd's Avatar
    Join Date
    Sep 2009
    Posts
    17
    As per the need of the hour,I want my program to be called b4 sudo apt-get clean.
    Thanks a lot for ur inputs.
    I will work with it (aliasing).

  5. #5
    Just Joined! kapmsd's Avatar
    Join Date
    Sep 2009
    Posts
    17
    How would I use alias to call my program when a command like "sudo apt-get autoclean" is gonna be executed??
    Using alias ,i can create only some shortcuts only for commands,right??

  6. #6
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Well, you "shortcut" sudo to your C program.
    This program would check the first and second parameter. If they are "apt-get" and "autoclean", appropriate action is taken before "sudo apt-get autoclean" is literally invoked. Otherwise, sudo is executed in the C programm with whatever parameters there be.
    Debian GNU/Linux -- You know you want it.

  7. #7
    Just Joined! kapmsd's Avatar
    Join Date
    Sep 2009
    Posts
    17
    GNU-Fan ,but your method works only in one terminal.
    When I open up another i have to start the whole aliasing process.
    Is there any better way to detect only the sudo apt-get clean command in ubuntu system??

  8. #8
    Just Joined! sixdrift's Avatar
    Join Date
    Jan 2007
    Location
    In and around and about Cary, NC
    Posts
    44
    Put the alias definitions in your ~/.bashrc file or ~/.bash_aliases (or other shell resource file as appropriate) so that it gets loaded automagically each time you start a new terminal window.

  9. #9
    Just Joined! kapmsd's Avatar
    Join Date
    Sep 2009
    Posts
    17
    @sixdrift: Thanks a lot for ur input

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...