Bash:变量Isn';t正确使用/定义

Bash:变量Isn';t正确使用/定义,bash,debian,Bash,Debian,正在处理我的自动安装脚本。它没有正确使用我的变量,调试日志如下所示 我尝试了几种变体,使用“”而不是“”,不使用“”或“”,只使用$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

正在处理我的自动安装脚本。它没有正确使用我的变量,调试日志如下所示

我尝试了几种变体,使用“”而不是“”,不使用“”或“”,只使用$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         

如果要在脚本中使用参数(基于上面的注释,“域”和“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\u LOG\u 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

您确定“域”设置正确吗?脚本的输出是什么。也可以考虑用'-u '(在第一行之前添加“设置-u”)来识别未定义的/拼写错误的变量。使用<代码> $()<代码>运行命令并捕获它的输出而不是回退(如果使用回扣,不要在前面放置一个美元符号)。另外,在变量引用周围加上双引号(例如,`echo“$MOREF是SQL密码”)。有些地方不使用它们是安全的(比如作业的右侧,有时除外),但通常只使用它们更安全。最后,我建议您对自己的东西使用小写或混合大小写变量,因为有一堆带有特殊函数的所有caps变量,错误地重复使用其中一个可能会导致麻烦。好的,还有一个注意事项:使用。它不会发现所有内容,但会指出许多常见错误。您好,我添加了完整的代码。基本上,它创建文件夹、文件和SQL命令。您说我应该使用$()来捕获输出。但是为什么运行时
MOREF=$`pwgen 14 1
会返回并正确显示结果?另外,如何编写脚本,以便在运行应用程序时设置变量?像这样:
root@hosting:/bin#hello.sh VARIABLE1 VARIABLE2
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 -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