Scripting question

S2S-Robert

Verified User
Joined
Jun 24, 2003
Messages
415
Location
The Netherlands
Hi

I'd like to create the scripts for domaincreation and usercreation and such. What I would like to have in them is the following noticed by another thread:

http://www.directadmin.com/forum/showthread.php?s=&threadid=198&highlight=~username

The idea I've come up sofar is:

* When a user is created remove the symbolic link public_html and create a directory instead. Then create a link inside that directory to point to the correct domain.
* When a domain is created, just create a link inside the directory.
* When the domain is removed remove the symbolic link and perhaps when the user is created delete everything (but I assume this is already done by DA)

This way I would have an automated setup of http://ip/~user/<domain.ext> instead of having to create them manually for multiple domains under 1 user.

Any thoughts?
 
Hello,

Should be quite straighforward and simple. I'm assuming you've found the custom scripts feature.

John
 
Yes I did, the only problem being that I'm not a linux programmer, so to be honest I don't even know what the .sh files really do.

If anybody could point me in the right direction and has a rough idea of what to put in there please do let me know!
 
Hello,

.sh files are bascially like a batch file. Anything that you type in at the command prompt can be duplicated in the sh file and when you run it, everything in there will be run as if it was you typing really fast :)

Example: domain_create_post.sh.sh file. Lets say you want to copy some files to their domains directory.. a php script perhaps. The domain_create_post.sh might look like the following:
Code:
#!/bin/sh

# #!/bin/sh has to exist on the first line all by itself.

# the # character is used for comments and
# everything after the # character is ignored.

# copy yourfile.php from somewhere on the server
# to the users' public_html directory
cp /somepath/yourfile.php /home/${username}/domains/${domain}/public_html/yourfile.php

# make sure that the correct person owns the file so
# that they can delete it and make alterations if they
# need to.
chown ${username}:${username} /home/${username}/domains/${domain}/public_html/yourfile.php
Save the file, and then run:
Code:
chmod 755 domain_create_post.sh
to ensure that it's executeable. You'll probably notice the ${username} and ${domain} values everywhere. Those are the environmental variables as defined in the README file in the ./scripts/custom directory. They'll be filled in to their rightful places.

There are also things like coding style to consider.. for example we wrote out the full path to the new file twice. This is generally "poor" style (but was done to simplify the example). A better way of doing it is to assign the full path to a new variable and then use that variable instead:
Code:
#!/bin/sh
NEWFILE=/home/${username}/domains/${domain}/public_html/yourfile.php
cp /somepath/yourfile.php ${NEWFILE}
chown ${username}:${username} ${NEWFILE}
This saves a lot of typing and also ensures that both copies are typed the same. It also makes it far easier to change the value of the new file everywhere in the script by only changing it in one place.

Anyway, this is a very quick and dirty "how-to" for creating an .sh script.

John
 
Ok, since I'm no bash guru either and basically learning by doing here's what I've come up with:

user_create_post.sh

Code:
rm /home/$[username]/public_html -f
mkdir /home/$[username]/public_html
chown $[username]:$[username] /home/$[username]/public_html

domain_create_post.sh

Code:
cd /home/$[username]/public_html
ln -s ../domains/$[domain]/public_html $[domain]

So could this work?
 
Hello,

Just a few minor things:

user_create_post.sh
Code:
PUBHTML=/home/${username}/public_html
rm -f $PUBHTML
mkdir $PUBHTML
chown ${username}:${username} $PUBHTML

domain_create_post.sh
Code:
cd /home/${username}/public_html
ln -s ../domains/${domain}/public_html ${domain}
Just changed [] to {} and added the path to a variable.

John
 
thanks, that sure is helpfull.

I have some questions left though:

* If I create the user will domain_create_post.sh also run after adding the domain or is it integrated in the user_create_post.sh?
* As what user is this script running (root?)
 
Hello,

Since a domain is created during the user creation, both scripts will be run. The create_domain_post.sh will be run before the create_user_post.sh.

The script is running as root (so be careful :))

John
 
So if the domain_create is run before the user create then it's quite senseless to remove the symlink in the user create right? I assume that's why domain_create is giving me the problems, it's trying to put a symlink inside an existing symlink.

Is there some way to check if public_html is in fact a symlink and if it is remove it?
 
you could use ls to check, with shell you end up with a non zero value or a 0 depending on if the commnds were successful or not, example script below:

Code:
pico -w tester.sh

Code:
#!/bin/sh

##############################################################
# Example script to check if a file exists or does not       #
##############################################################

ls $1 > /dev/null 2> /dev/null
if test $? -eq 0
then
# if the file exists run any commands between here....
echo "File [$1] exists"
# ...and here
else
# if the file does not exist
echo "File [$1] does NOT exist"
fi

Code:
chmod 755 tester.sh

Example Usage:

./tester.sh /path/to/existant_file
./tester.sh /path/to/nonexistant_file

The output should simply be:

File [existant.php] exists

File [nonexistant.php] does NOT exist


Chris
 
Back
Top