0

I've been working on this little script and it's nearly at a point that I can start using it.

The idea is to automate WordPress installs via Plesk.

I've got Plesk setup with a cronjob to download the latest.tar.gz from wordpress.com and extract it every morning and now I'm trying to get the database creation side of things automated.

I have the following bash script:

#!/bin/bash/

#To whoever is working on this
#this script simply sets up the DATABASE for the new domain added to the server
#it then logs it, mails the details and clears the log


genpasswd() {
 local pass=`cat /dev/urandom | tr -cd "[:punct:]" | head -c 2`
    echo "$pass"
}

echo $NEW_DOMAIN_NAME >> /usr/games/newdomain.txt
DOMAINNAME=`echo $NEW_DOMAIN_NAME | grep -oE '/[^/]+' | cut -c6- | rev | cut -c4- | rev`
rndChr=$(genpasswd)

CHAR2=${$DOMAINNAME:1:1}
CHAR4=${$DOMAINNAME:3:1}

echo $CHAR2$CHAR4 >> /usr/games/passChar.txt

#get our db stuff
DATABASENAME='exsite_'$DOMAINNAME
DATABASEUSER='exsite_'$DOMAINNAME
DATABASEPASS='[exSite_'$CHAR2$rndChr$CHAR4']'


#create our mail content
CONTENTS="<strong>New MySQL Database created</strong> - <i>see the login details below.<i><p><strong>dbname</strong>    $DATABASENAME<br><strong>dbuser</strong>    $DATABASEUSER<br><strong>dbpass<strong> $DATABASEPASS"

#log it
echo -e $CONTENTS > /usr/games/dbLog.txt


#mail it
sendMail() {
cat << 'EOF' - /usr/games/dbLog.txt | /usr/sbin/sendmail -t
To: servers@exsite.ie
From: Enterprise
Reply-to: alex@exsite.ie
Subject: [DB Info] for $NEW_DOMAIN_NAME on enterprise.exsite.co
Content-Type: text/html

EOF

printf "STATUS: Mail sent!"

}

if [[ $(sendMail) ]]
then 
    printf "STATUS: Doing mail thing!"
else
    printf "STATUS: MAIL-FAIL!!"
fi

#clear dbLog.txt
exit

I know it's not the tidiest script but it's a work in progress and my first bash script so bare with me.

Two problems are occuring but I don't understand why;

The CHAR2 and CHAR4 variables don't seem to pick up the 2nd and 4th characters of $DOMAINNAME - they just output empty into passChat.txt

$NEW_DOMAIN_NAME goes to newdomain.txt but then the next line where I try to return just the 2nd level domain seems to fail. It was working at an earlier point but it's not now. I hadn't changed that part though.

  • The syntax for the more "advanced" parameter substitution does NOT include the dollar inside the braces (ref)-- CHAR2=${DOMAINNAME:1:1} – glenn jackman Apr 24 '14 at 20:47
  • What does $NEW_DOMAIN_NAME look like? I can't really grok what that grep|cut|rev|... line is supposed to do – glenn jackman Apr 24 '14 at 20:48
  • $NEW_DOMAIN_NAME is supposed to be mydomain.com and the CHAR2=${DOMAINNAME:1:1} should get me the 2nd character, so in this case, should hold the character 'y' from 'mydomain.com' and CHAR4 should hold 'o' – Mud Apr 24 '14 at 20:51
  • oh and the grep|cut|rev is supposed to strip the www. and the .com from the domain name although they're coming through now without www. so maybe that's the problem? – Mud Apr 24 '14 at 21:01
1

Assuming you want to get "mydomain" whether you are given any of: "mydomain.com", "www.mydomain.com", "mail.secure.mydomain.com", ...

get_domain_name() {
    local IFS=.
    set -- $1
    local n=$(($# - 1))
    echo ${!n}
}

get_domain foo.bar.baz.qux
baz

The proper way to check the results of your sendmail function is with a lot less syntax:

if sendmail
then echo OK
else echo uh oh
fi

if checks the exit status of a command. Note that [[ is a bash builtin command (that requires its last argument to be ]]) -- at a bash prompt, type help [[ if


In your sendmail function, you use cat << 'EOF' -- that effectively single-quotes the entire here-doc. Any variables will not be substituted.

You appear to want to contatenate the heredoc, then stdin, then a named file, and then pipe all that to sendmail. You'll need a bit more syntax to do it: use the { grouping } braces to concatenate the info, then pipe the result to sendmail

sendMail() {
    {
        cat <<- HEADERS
            To: servers@exsite.ie
            From: Enterprise
            Reply-to: alex@exsite.ie
            Subject: [DB Info] for $NEW_DOMAIN_NAME on enterprise.exsite.co
            Content-Type: text/html

            HEADERS
        cat -
        cat /usr/bin/games/dbLog.txt
    } | /usr/sbin/sendmail -t && echo "STATUS: Mail sent!"
}

Note here I used a heredoc with <<- to allow the use of leading tabs to indent document. bash is strict about only leading tabs, not spaces, so be careful about that.


Are you aware that printf does not append a newline to the outputted string? All your messages are going to be squished together on one line.

  • Thank you so much :) I will make the appropriate changes you've suggested and see where that gets me. – Mud Apr 24 '14 at 22:55
  • This happens when I do your sendmail function and I can't figure out what the error means? You have the blank line after the terminating HEADER? Why doesn't it see that as the EOF? newDB.sh: line 66: warning: here-document at line 46 delimited by end-of-file (wanted HEADERS')` newDB.sh: line 67: syntax error: unexpected end of file – Mud Apr 24 '14 at 23:31
  • 1
    Make sure there are only tab characters before the terminating HEADERS. – glenn jackman Apr 25 '14 at 9:52
  • 1
    I rarely use <<- just for this potential problem. It's hard to maintain only tabs. My vim setup uses spaces for indentation so it's extra effort to ensure tabs. – glenn jackman Apr 25 '14 at 14:01
  • The only thing now is that the get domains function doesn't seem to work - it just returned an empty string? I just rewrote my grep|rev|cut|rev command for now but I realized it only works for .com domains (or any 3 letter tlds) but not for the likes or .ie or .co.uk Still it's good enough for now :) Thanks again! – Mud Apr 25 '14 at 14:14

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.