0

What I want to do: Whenever the apt-get install command is run and terminates, run another command immediately without any involvement on the user's part. In my case I want to run a script, but since I can substitute a command for that, a command would be preferable as an example.

By the way, I don't want to run a script that executes two commands, since it must then be manually executed, which is not what I want. The termination of the apt-get install command must prompt the execution of another command.

NOTE: This may seem like a duplicate of this question, and while I do have an answer, I want to make my intention clear in case there are any other possible solutions, while also clarifying my requirements to other potential viewers to this question.

Edit:

To clarify: I am trying to get one command that runs two!
Why? I want to generate a file containing a list of all the installed dependencies and software on my distro, which is updated every time the apt-get install command is used.

13
  • Unclear?? I don't get it - the question is as clear as possible!
    – TellMeWhy
    Nov 25, 2015 at 21:19
  • 1
    this question is clear, but it may well be a duplicate, though i cannot find one. I'll be posting an answer in a second Nov 25, 2015 at 21:20
  • @Serg So why does it have two votes to close this question as being "Unclear"?
    – TellMeWhy
    Nov 25, 2015 at 21:20
  • 1
    @DevRobot are you trying to get one command that runs two ? Nov 25, 2015 at 21:31
  • 1
    @devRobot OK, editing my answer in a second Nov 25, 2015 at 21:32

2 Answers 2

3

The hard part is parsing the arguments correctly. You can handle simple apt-get install pkgname cases easily, but as soon as you want to handle apt-get -y install pkgname in addition, you suddenly need a full fledged option parser.

Assuming the simple cases is ok enough, something like this wrapper script may suffice:

#!/bin/bash

/usr/bin/apt-get "$@"
status=$?

if [[ $1 = install ]]; then
    read -rp 'Run this other thing too [Y/n]? ' ans
    if [[ ${ans:-y} = [Yy] ]]; then 
        other-thing
        exit # exit with other-thing's exit status.
    fi
fi

exit "$status" # exit with apt-get's exit status.
1

There's couple of ways, depending on your intention.

Sequential execution can me done with ; separator. It will be done in sequence, each command runs regardless for previous command's exit status. Example:

echo hello; printf "\n\n%s" WORLD

Conditional execution can be done with boolean operators || and &&. With these, execution of the second program, depends on the exit status of first (first and second read left to right).

For && the behavior is as such: COMMAND1 && COMMAND2 , if COMMAND1 successfully terminated, COMMAND2 will run. You can remember this as "if left succeeds, the right will run; if left failed, right won't run" [ -f /etc/passwd ] && echo "/etc/passwd exists"

For || it's opposite "If left succeeds, right won't run; if left fails, right will run"

[ -d /etc/passwd ] || echo "/etc/passwd is not a directory"

To put these into one single command, there is couple of options. One obviously is script, but as you requested, you don't want a script. Alternative to that is bash function OR alias.

Bash function is preferred since you can pass arguments to them if you so desire.

In your specific case, you want apt-get install which may take multiple parameters

function installStuff
{
  apt-get install "$@"
  command2
}

Place that into your .bashrc, somewhere at the top, close .bashrc and run source .bashrc. Now you have a bash function that always runs apt-get install first, with any number of packages requested, and then immediately command2

Like so

installStuff package1 package2 package3

For conditional running of command2 , use the && and || operators i've already mentioned.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .