6

Here's an example of a very simple code snippet that I'd like to paste to my terminal in a way that everything is executed.

sudo apt update
sudo apt upgrade


sudo apt -y install build-essential
sudo apt -y install git
sudo apt -y install libxml2-dev # required for some tools using xml files

sudo apt autoremove

Unfortunately, what happens if build-essential wasn't installed beforehand is that it only runs until sudo apt -y install build-essential. The subsequent lines are skipped. The same is true if git wasn't installed: It'll run until the git line, then skip the rest.

What's the reason for this happening, and is there a way to fix this problem without having to create a script file and running it via bash?

11
  • 1
    Trying to understand... If the build-essential were to fail, would you want to continue executing the other items? Commented Mar 26, 2019 at 15:22
  • Also, since you're (probably) running these commands via bash, is there a particular problem with creating a script and running that? Commented Mar 26, 2019 at 15:24
  • Basically, I'm just trying to understand what it is exactly that prevents this snippet from being fully executed (line by line) by pasting it into the Unix terminal. Commented Mar 26, 2019 at 15:30
  • I have a collection of Unix shell snippets and I need to have a basic understanding of when pasting snippets into the terminal works, and when it doesn't. Commented Mar 26, 2019 at 15:31
  • 1
    The situation I'm imagining is where sudo apt -y install build-essential is prompting for some specific response (yes, no, something) and it gobbles up your subsequent pasted lines as garbage responses to its prompts, thus never completing. Commented Mar 26, 2019 at 15:41

2 Answers 2

6

Assuming you are still within sudo's credential cache timeout (if you are unsure, just refresh it with sudo -v before running the snippet), that problem happens because apt(-get) is a very rich console application and thus consumes stdin even when it asks you nothing because of the -y.

You can work around that by running the whole snippet in a subshell:

At the prompt, start by typing a ( then paste the snippet then type the closing ) and press return

It should go.

Notice how the snippet is not executed as soon as you paste it. It rather gets “queued” on the command line, waiting for the closing parentheses.

(PS: depending on your system you may need to use apt-get autoremove in place of apt autoremove, and you may also need to use -y on update and upgrade too)

3
  • 1
    Awesome, that did it! So basically sudo and apt are complex commands that have a tendency to eat up input. Gotcha. Just one comment: apt autoremove exists and works. Also, an alternative workaround I found is to concatenate the commands via && (ensuring the next line always is only executed if the previous succeeded) and to either write all commands on a single line, or to use the escape character \. Commented Mar 26, 2019 at 16:29
  • ugh.. It's really about time to update my system then.. apt autoremove does not work for me..! :-( Commented Mar 26, 2019 at 16:41
  • @Michael Of course those ways and a few others work too, but you wanted to just copy&paste it ;-) Commented Mar 26, 2019 at 16:42
1

If there is sufficient time between the sudo apt upgrade command and the sudo apt install -y build-essentials command, then SUDO will prompt for your password again.

Because you are pasting text into the console, the next line(s) will be accepted as STDIN to the SUDO prompt for a password. If the line does not match your password, authentication will fail and the build-essentials line will not be executed.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.