3

I'm currently working on making a text file of the whole process of setting up my server for my normal usage.

And would like to know if there is any way to write a shell script so that I can make the "nano" insert content, save it, exit it, and then continue with the rest of my commands.

Cause when I put all of this into the shell script:

sudo nano /etc/apache2/conf-available/fqdn.conf
ServerName localhost
sudo a2enconf fqdn

Then it shows the "nano" file editor with this content:

ServerName localhost
sudo a2enconf fqdn

Well, while I wanted it to save and exit after the ServerName localhost, so that my script could continue.

Would that be posibble anyhow?

6

I may have misunderstood your question. SHH doesn't exist, do you mean SH or SSH? Or maybe Bash? (EDIT: brought an edit to fix this)

Anyway, nano won't be much of assistance here. echo will.

echo "ServerName localhost" > /etc/apache2/conf-available/fqdn.conf
sudo a2enconf fqdn

The above shell code will send your text into the file. Replacing > with >> will append it instead, if that's what you're looking for.

If you need root privileges to write to this file, then use tee instead of a simple shell redirection (>/>>). This will allow you to use sudo properly:

echo -e "ServerName localhost" | sudo tee -a /etc/apache2/conf-available/fqdn.conf
sudo a2enconf fqdn

nano is here so that you, the user, can write data manually into a file. However, if you're looking for an automated process, there's no need for an editor, coreutils can handle that just fine. Linux doesn't need the fancy nano interface to write into a file.

3
  • This was exactly what I wanted using SSH. Thanks and sorry for the misunderstanding. Jul 27 '14 at 12:31
  • 1
    This is not SSH, this is shell programming, using /bin/sh or /bin/bash. Jul 27 '14 at 12:32
  • 2
    @JohnWHSmith +1 for almost clairvoyant abilities.
    – guntbert
    Jul 28 '14 at 9:21

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.