Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I often write up wiki instructions to install various server packages on Ubuntu (11.10 Oneiric at the moment). They always involve things like:

sudo apt-get install -y postfix
sudo cp ~/siteconfig/etc/postfix/main.cf /etc/postfix

but when you cut and paste this to a terminal, either sudo, apt-get, or some subshell randomly swallows the subsequent lines of input, and only the apt-get install happens.

Is there a way to make this more cut-and-paste-friendly? I suppose I could wrap each section with

cat > script <<EOF
apt-get install -y postfix
cp ~/siteconfig/etc/postfix/main.cf /etc/postfix
EOF
sudo sh ./script

but is there a better way?

share|improve this question
    
Use a configuration management tool for such things? –  ℝaphink Dec 20 '11 at 13:27

6 Answers 6

up vote 6 down vote accepted

A way to both avoid the cut-and-paste problem, as well as safely run the commands in succession is to put them on the same line separated by && which will only execute the cp on the successful completion of the sudo apt-get install:

sudo apt-get install -y postfix && sudo cp ~/siteconfig/etc/postfix/main.cf /etc/postfix

Afterall, if the first command fails you probably don't want to continue executing the rest of the commands.

As for why the commands get swallowed when you paste multiple lines at once... when postfix gets installed it asks configuration questions with the debconf dialog frontend which is likely what's interfering with the cut-and-paste. Maybe a different front-end like readline or noninteractive would interfere less? Still, I would use the && method anyway since it is safer.

If you're installing postfix with your scripts it sounds like maybe you're trying to automate the install of new systems? If so, consider using preseeding as an option (here is some Ubuntu 11.10 specific documentation) or maybe use puppet?

share|improve this answer
1  
Yeah, I'm moving toward a config tool (and I am actually using preseeding for some things), but I figure this is a general case that will keep happening - there will always be multiline wiki instructions. I suppose I could add && \ to the end of each line, maybe that's the best way.. –  Jay Levitt Dec 20 '11 at 17:45
    
Shell scripting is great for quick and dirty stuff, but I'd rather write scripts using Python / Fabric / Puppet which together builds a much more manageable system with better exception / error handling and a saner syntax than what you have to deal with in monstrous shell scripts. –  aculich Dec 20 '11 at 21:52

There is an outstanding bug report about this: Debian Bug report #728775 - apt-get unwarrantedly consumes input. The report says:

When apt-get is invoked in a way that involves actually installing a package, it reads any available data from standard input, regardless of actual need. This breaks the usual ability, at an interactive shell, to type the next command while the current one is running: apt-get consumes input that was intended for the shell. strace shows that the input is read by the top-level apt-get process, by read(2) from fd 0, in response to pselect(2).
...
It's in pkgDPkgPM::Go() in apt-pkg/deb/dpkgpm.cc. Further down the same function, there's a pselect loop which does match what I saw with strace. It reads from stdin conditional only upon (master >= 0 && !d->stdin_is_dev_null)

And in a response:

It's to avoid someone typing their next command while packages are downloading, but then that input being used as the answer to a prompt during installation.

A workaround for you, since you're already giving apt-get the -y option so presumably don't want it to ask questions or read responses, is to redirect stdin to /dev/null. When I do the following:

apt-get install -y gdb-doc < /dev/null

my typeahead during execution of the apt-get command is not flushed.

share|improve this answer

I'm not sure why this is happening with your paste. It doesn't happen to me.

Perhaps the solution is social, rather than technical.

Write your commands on a line by themselves, with documentation separating them:


First, we install postfix:

    sudo apt-get install -y postfix

Then, we fetch its configuration:

    sudo cp ~/siteconfig/etc/postfix/main.cf /etc/postfix

If people are forced to cut-and-paste line by line, the "subsequent line" problem goes away. And more documentation is never a bad thing. :)

share|improve this answer
    
Please don't do this. Being forced to manually copy line by line is such a pain. If you intersperse documentation, please use shell-comments. –  SystemParadox Aug 21 '12 at 9:56
    
@SystemParadox - then the OP's original problem remains. Doing your system administration by cut-and-paste is a bad idea. If it doesn't work in the first place for the OP, then so much the better. Making it a pain helps to discourage the practice. –  ghoti Aug 21 '12 at 11:09

For apt-get I find that yes works better than -y:

yes | apt-get install postfix
share|improve this answer

If your problem is related to apt-get install command, you can try the following:

sudo apt-get install -y -q=2 postfix
sudo cp ~/siteconfig/etc/postfix/main.cf /etc/postfix
share|improve this answer
sudo apt-get install -y postfix  2>&1 |tee 
sudo cp ~/siteconfig/etc/postfix/main.cf /etc/postfix 2>&1  |tee 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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