0

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)

3
  • Whats the error messages???
    – Arenstar
    Nov 17, 2010 at 18:57
  • What is the error that is being reported?
    – user9517
    Nov 17, 2010 at 18:59
  • sorry about that, I didn't even think about the error message, lol. It hasn't helped me at all though. Nov 17, 2010 at 20:02

1 Answer 1

9

Since you haven't posted an error message I'm just making an educated guess here. Your script does this:

# Set new password for user
exec expect -f $0 ${1+"$@"}

This calls expect with the full path to your script passed as the argument to expect's "-f" option...which means that expect will start executing from the top of your script, not from the "set" line immediately following your exec statement.

This will cause an immediate syntax error because the script is a shell script, not an expect script.

You probably want to move the expect script into a separate file, and then call it explicitly:

exec expect -f my-expect-script ${1+"$@"}

Also, and this isn't germain to your question, your script uses both position variables (e.g., "$1") as well as the $args list you initialize at the top of your script. You're effectively doing the same thing in two different ways, which will at some point lead to confusion and dismay.

2
  • thank you very much, I didn't realize that this is what the exec expect was doing. Referring to itself to be loaded as the expect script. I will rewrite it do do otherwise. I will post the resulting script that I have developed. Nov 17, 2010 at 19:24
  • Awesome guessing, deserved... +1 :D
    – Arenstar
    Nov 17, 2010 at 19:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .