I am trying to develop a script that will automate setting up a new virtual hosting domain on my server, I have a custom setup on my server that gives every domain a username and password but no shell access. (this is do to vsftpd using system users) It also creates a directory in the /var/www/sites/"domainname.com" with all of the default files in it. Then created a /etc/apache2/sites-available/domaindname.com Apache Conf file for the Virtual Host. This script errors but still looks as if it completes everything. I would like the error to go away though.
#!/bin/bash
#\
args=("$@")
# Copy Site Hosting Directory
cp -a /var/www/sites/skeleton /var/www/sites/${args[0]}
# Copy Apache Config File
cp -a "/etc/apache2/sites-available/skeleton" "/etc/apache2/sites-available/$1"
# Replace 'skeleton' with '$1' in config file
sed -i "s/skeleton/$1/g" /etc/apache2/sites-available/$1
# Create User
useradd -d /var/www/sites/$1 -s /usr/sbin/nologin $1
chown -R $1:www-data /var/www/sites/$1
chmod -R 0775 /var/www/sites/$1
# Set new password for user
exec expect -f $0 ${1+"$@"}
set password [lindex $argv 1]
spawn passwd [lindex $argv 0]
sleep 1
expect "assword:"
send "$password\r"
expect "assword:"
send "$password\r"
expect eof
The error message I get is:
can't read "args[0]": no such variable
while executing
"cp -a /var/www/sites/skeleton /var/www/sites/${args[0]}"
(file "./test" line 5)
Script Solution: (thanks to the help of the serverfault users)
#!/bin/bash
# Copy Site Hosting Directory
cp -a /var/www/sites/skeleton /var/www/sites/$1
# Copy Apache Config File
cp -a "/etc/apache2/sites-available/skeleton" "/etc/apache2/sites-available/$1"
# Replace 'skeleton' with '$1' in config file
sed -i "s/skeleton/$1/g" /etc/apache2/sites-available/$1
# Create User
useradd -d /var/www/sites/$1 -s /usr/sbin/nologin $1
chown -R $1:www-data /var/www/sites/$1
chmod -R 0775 /var/www/sites/$1
# Set new password for user
# /usr/bin/expect -f $0 ${1+"$@"}
expect -c "
spawn passwd $1
sleep 1
expect \"assword:\"
send \"$2\r\"
expect \"assword:\"
send \"$2\r\"
expect eof
"
The problem is that the expect was doing a recursive call on itself, but the file was a mixed bash and expect script. So the solution is to call expect with a -c (string to be parsed and executed)