Results 1 to 9 of 9

Thread: bash script quotation marks

  1. #1
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    bash script quotation marks

    I am writing a bash script that generates other scripts,
    however one of these scripts needs quotations ["];
    the end goal is to amend bind9 config files to add more domains automatically.

    quotations will close my 'echo' statement, so can I use something else in my 'echo'?

    echo "zone "$sitename" {
    type master;
    file "/etc/bind/db.$sitename";
    };
    "
    can I use another symbol for these quotation marks in green? or even use doubled up double quotation marks?
    Last edited by Toriku; January 4th, 2013 at 03:50 PM.

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: bash script quotation marks

    If you need literal quotes you can backslash escape them

    Code:
    $ echo "Here is a \"quoted string\""
    Here is a "quoted string"
    However if you are trying to prevent word splitting of the variable, the outer quotes are usually sufficient I think

    Code:
    $ var="quoted string"; echo "Here is a $var"
    Here is a quoted string

  3. #3
    Join Date
    Jul 2011
    Beans
    18

    Re: bash script quotation marks

    You can can try this:

    echo "zone \"$sitename\" {
    type master;
    file \"/etc/bind/db.$sitename\";
    };
    "

  4. #4
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    Re: bash script quotation marks

    thanks for the help, I'll give it a go

  5. #5
    Join Date
    Jul 2009
    Beans
    516
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: bash script quotation marks

    You could also do that by means of a HEREDOC which makes it a little easier to read. See here
    Code:
    sitename=mydomain.com
    
    cat <<EOM > /tmp/db.$sitename
    zone "$sitename" {
            type master;
            file "/etc/bind/db.$sitename";
    };
    EOM
    Mark your thread as [SOLVED], use Thread Tools on forum page.

  6. #6
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    Re: bash script quotation marks

    that seems to have worked, but my website didn't...
    I can't seem to figure out why, all the files have been generated,
    and seem to be ok, but I can't access the website I made...
    I tried rebooting the server and my PC, to make sure it was set up right...

    can anybody spot what I may have missed out in setting up this webpage?
    I have other websites I have set up manually that work fine,
    but the website I generated with this script doesn't work...

    #!/bin/bash
    # this script once complete should automatically place
    # the files to generated a new website, into the virtualhost
    # of apache, it should also add the DNS settings and generate
    # a generic index file to start the website off.

    #first lets check that the user is in superuser
    uid=`id -u $USERNAME`
    if [ "$uid" == "0" ]
    then
    echo "user is root"
    else
    echo "you need to have root access, run again in sudo"
    exit 1
    fi
    echo "starting program"

    # -----------------------------
    # now that formality is out of the way, lets start by clearing some workspace
    clear

    # collect website name
    echo "please enter the domain name of website address"
    echo "without www; in format 'example.com'."
    read webname
    echo "ok, creating website www.$webname"
    echo ""
    # create website directory
    mkdir /var/www/www.$webname

    # create index file for website
    echo "<h1>Index file for $webname</h1>
    <p>this is the autogenerated index file for www.$webname<br>
    there is no content yet, please upload it</p>" > /var/www/www.$webname/index.html

    # state that the website has been generated
    echo "website files generated"

    echo "generating virtual apache webserver files..."
    # add the settings for the new virtual server into apache
    echo "<VirtualHost *:80>
    ServerAdmin webmaster@$webname
    ServerName www.$webname
    ServerAlias $webname

    # Indexes + Directory Root.
    DirectoryIndex index.html
    DocumentRoot /var/www/www.$webname/

    # CGI Directory
    ScriptAlias /cgi-bin/ /var/www/www.$webname/cgi-bin/
    <Location /cgi-bin>
    Options +ExecCGI
    </Location>
    </VirtualHost>" > /etc/apache2/sites-available/www.$webname
    # ---------------------------------------------------------
    #
    # -- the website-available file has been generated, lets activate it
    echo "****running a2ensite****"
    a2ensite www.$webname
    echo "****completed a2ensite****"
    # -- announced that the script has run
    echo ""
    echo "adding the DNS entries..."

    # ----------------------------------------------
    echo "zone \"$webname\" {
    type master;
    file \"/etc/bind/db.$webname\";
    };"
    # ----------------------------------------------
    echo "ammended named.conf.local"


    # generate database file
    echo "generating database file db.$webname"
    echo ";
    ; BIND data file for local loopback interface
    ;
    $TTL 604800
    @ IN SOA vicci.$webname. root.$webname. (
    1001 ; Serial
    604800 ; Refresh
    86400 ; Retry
    2419200 ; Expire
    604800 ) ; Negative Cache TTL
    IN A 192.168.1.84
    ;
    @ IN NS ns.$webname.
    @ IN A 192.168.1.84
    @ IN AAAA ::1
    ns IN A 192.168.1.84
    " > /etc/bind/db.$webname
    echo "database file written, website generation complete!"
    # script completed, rebooting daemons
    echo "******************"
    echo "restarting daemons"
    echo "******************"

    service bind9 restart
    service apache2 reload
    service apache2 restart

    echo "daemons have been reset,"
    echo "it may be necesary to restart your browser or"
    echo "reboot your PC in order to view them!"
    echo "..if the page still won't load you may need to reboot the server"

  7. #7
    Join Date
    Jul 2009
    Beans
    516
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: bash script quotation marks

    What doesn't work about it?? - have you checked the output of the files you're creating? have you made sure your client is able to resolve the hostname correctly..?

    For a start you're not saving the zone to your apache conf ->
    Code:
    # ----------------------------------------------
    echo "zone \"$webname\" {
    type master;
    file \"/etc/bind/db.$webname\";
    };"
    # ----------------------------------------------
    echo "ammended named.conf.local"
    You need to redirect the output like you did for your zone data file. i.e.
    Code:
    ... >> /etc/bind/named.conf.local
    Also, where you generate the zone data file $TTL will be evaluated as a variable as it's in double quotes, you need to escape the $ sign as in
    Code:
    \$TTL 604800
    Mark your thread as [SOLVED], use Thread Tools on forum page.

  8. #8
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    Re: bash script quotation marks

    Quote Originally Posted by btindie View Post
    What doesn't work about it?? - have you checked the output of the files you're creating? have you made sure your client is able to resolve the hostname correctly..?

    For a start you're not saving the zone to your apache conf ->
    Code:
    # ----------------------------------------------
    echo "zone \"$webname\" {
    type master;
    file \"/etc/bind/db.$webname\";
    };"
    # ----------------------------------------------
    echo "ammended named.conf.local"
    You need to redirect the output like you did for your zone data file. i.e.
    Code:
    ... >> /etc/bind/named.conf.local
    Also, where you generate the zone data file $TTL will be evaluated as a variable as it's in double quotes, you need to escape the $ sign as in
    Code:
    \$TTL 604800
    well spotted, I had amended the part that amended the zone file in the apache conf as it was including a comment; I forgot to add the ">> /etc/bind/named.conf.local" back on...

    and you are quite right, the "$TTL" was missing from the db file; I'm going to see if these changes have done the trick

  9. #9
    Join Date
    Nov 2012
    Location
    Great Britain
    Beans
    80

    Re: bash script quotation marks

    Perfect!!!

    everything seems to work now, thanks for your help!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •