使用我的自动安装脚本。它没有正确使用我的变量,调试日志如下所示。
我尝试了几种变体,使用“”而不是“”,而不是使用“或”,而只是使用$ variable。脚本如下所示:
echo "Creating Apache2 VHost and Wordpress blog $domain"
VAR1=$sitealias
VAR2=$domain
MOREF=$`pwgen 14 1`
echo $MOREF is SQL password.
mkdir /var/www/"$domain"
chown -R www-data:www-data /var/www/"$domain"
echo "<VirtualHost *:80>
ServerName myblog.example.com
ServerAdmin webmaster@example.com
DocumentRoot /usr/share/wordpress
Alias /wp-content /var/lib/wordpress/wp-content
<Directory /usr/share/wordpress>
Options FollowSymLinks
AllowOverride Limit Options FileInfo
DirectoryIndex index.php
Require all granted
</Directory>
<Directory /var/lib/wordpress/wp-content>
Options FollowSymLinks
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
" > /etc/apache2/sites-available/$domain.conf
mysqladmin -u root password supersecretpassword create wp_$sitealias
mysqladmin -u root password supersecretpassword CREATE USER $sitealias@'localhost' IDENTIFIED BY '$MOREF';
mysqladmin -u root password supersecretpassword GRANT ALL PRIVILEGES ON wp_'$sitealias.'* TO '$sitealias'@localhost;
FLUSH PRIVILEGES;
quit
脚本应将变量插入命令的文件夹目录中。而是如调试日志中所示,仅忽略变量。
更新:
变量捕获现在可以正常工作了。但是,SQL不喜欢您的双引号(复制并粘贴)
root@hosting:/bin# hello.sh letstest.tld testtesttdjsuqwu
Creating Apache2 VHost and Wordpress blog
qui2AenaihoQu9 is SQL password.
mysqladmin: Unknown command: 'create wp_testtesttdjsuqwu'
mysqladmin: Unknown command: 'CREATE USER 'testtesttdjsuqwu'@'localhost' IDENTIFIED BY 'q'
mysqladmin: Unknown command: 'GRANT ALL PRIVILEGES ON wp_testtesttdjsuqwu.* TO 'testtesttd'
/bin/hello.sh: line 37: FLUSH: command not found
/bin/hello.sh: line 38: quit: command not found
答案 0 :(得分:0)
如果要在脚本中使用参数(根据上面的注释,“ domain”和“ sitealias”),请使用'$ 1''$ 2'来捕获它们
domain=$1
sitealias=$2
# Fixed quoting of pwgen)
MOREF=$(pwgen 14 1)
# Rest of the script here, using $domain, $sitealias
echo $MOREF is SQL password.
mkdir /var/www/"$domain"
尚不清楚您要'APACHE_LOG_DIR'的值是什么。假设需要基于Apache的默认值在运行时解析此变量,因此您想对'echo'语句使用单引号而不是双引号。这将延迟此变量的解析。
echo '<VirtualHost *:80>
ServerName myblog.example.com
ServerAdmin webmaster@example.com
DocumentRoot /usr/share/wordpress
# Lines removed for
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
' > /etc/apache2/sites-available/$domain.conf
此外,考虑对mysqladmin setup命令使用双引号。使用双引号启用变量替换,在mysql需要时使用单引号。
mysqladmin -u root password supersecretpassword "create wp_$sitealias"
mysqladmin -u root password supersecretpassword "CREATE USER '$sitealias'@'localhost' IDENTIFIED BY '$MOREF'";
mysqladmin -u root password supersecretpassword "GRANT ALL PRIVILEGES ON wp_$sitealias.* TO '$sitealias'@'localhost';"
FLUSH PRIVILEGES;
quit