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?

link|improve this question

75% accept rate
Use a configuration management tool for such things? – Raphink Dec 20 '11 at 13:27
feedback

3 Answers

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 anyway.

As for the 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.

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

link|improve this answer
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
feedback

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
link|improve this answer
feedback

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. :)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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