-5

Example: Once the add-apt-repository process has terminated, immediately run apt-get update - without having to enter the command manually.

Is there a way to do this? Is it possible to set such a combination without the use of the sudo password? (although I don't mind either way)

I'm asking the following: How can I link two commands together, so that whenever I execute a particular command, it always executes the the linked command after it?

  • 5
    You do know how to make a script? – Rinzwind Nov 25 '15 at 11:21
  • Refer here and here – Ravan Nov 25 '15 at 11:22
  • You don't understand my question: How can I link two commands together, so that whenever I execute a particular command, it always executes the the linked command after it? – TellMeWhy Nov 25 '15 at 11:29
  • What do you exactly mean by "link"? It seems to me that what you want is a new command that calls the first, on success it calls the second, on success then maybe a third and so on. – dadexix86 Nov 25 '15 at 11:34
  • 4
    It would have helped a lot if you'd explain why you want it and what is the context. You simply leave people guessing a solution to an unclear problem. – Jacob Vlijm Nov 25 '15 at 11:44
4

There are various issues hooking into this with an alias or a simple bash function, but you can write a little wrapper for a command and stick it in /usr/local/bin which takes priority in your path.

This is a little trick to create a script and chmod it at the same time.
It'll dump an executable wrapper script in /usr/local/bin/add-apt-repository.

sudo install -b -m 755 /dev/stdin /usr/local/bin/add-apt-repository << 'EOF'
#!/bin/sh
/usr/bin/add-apt-repository "$@"
apt-get update
EOF 

To reverse this, just delete /usr/local/bin/add-apt-repository.

If you want to do something fruitier (like linking apt-get install and apt-get update so you always update before installing), we just need to expand the script to look at the arguments.

sudo install -b -m 755 /dev/stdin /usr/local/bin/apt-get << 'EOF'
#!/bin/sh
if [ "$1" = "install" ] ; then
    /usr/bin/apt-get update
fi
/usr/bin/apt-get "$@"
EOF 

Though consider that this will run an update before every apt-get install call. This might sound helpful but could be really tedious in practice.

  • What if I chose another command, such as apt-get install? do I just replace the add-apt-repository bit? – TellMeWhy Nov 25 '15 at 11:40
  • 1
    apt-get install is not a command, it's a command and an argument. You can supplant apt-get in a similar way to this (save a script as /usr/local/bin/apt-get and have it call /usr/bin/apt-get) but your logic for calling things based on the arguments will be different from this script. – Oli Nov 25 '15 at 11:46
  • how about ~/.local/bin ? – xiaodongjie Nov 25 '15 at 11:57
  • @xiaodongjie Nope. ~/bin/ and ~/.local/bin/ aren't in the path for the sudo environment. – Oli Nov 25 '15 at 12:01
  • of course, but the script's contents includes sudo command in ~/.local/bin. @Oli – xiaodongjie Nov 25 '15 at 13:01
4

As normally,

sudo bash -c "command1; command2"

If you want to run command2 only after command1 is successfully completed, you can use as && rather than ;.

sudo bash -c "command1 && command2"

I'll use following command for your example.

sudo bash -c "add-apt-repository <repo> && apt-get update"
1

To execute certain commands in a particular sequence shell scripting is used. All you have to do is - Open terminal by pressing Ctrl+Alt+T And follow the steps -
1. Type gedit filename.sh
2. After entering this command a text editor will open, where you can type the required commands one on each line. For example,
sudo add-apt-repository
apt-get update
3. After typing all the commands, save and exit either by using GUI or press Ctrl+S and Ctrl+Q.
4. Now you will return back to terminal. Run sh filename.sh on terminal and your commands will execute, provided there is no error in your commands.
Also, if you login from superuser account (root) you won't have to type sudo in your command.

  • 1
    Did you read any of the other answers and comments? – Jacob Vlijm Nov 25 '15 at 12:40
  • @JacobVlijm Yes I did. But I guess they were rather complicated. – Prachi Nov 25 '15 at 12:46
0

The easiest way to do it is to run:

sudo add-apt-repository <ppa_name> && sudo apt-get update

If the first command is finished successfully, the second will be run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.