“:错误的变量名称读取:网站”

“:错误的变量名称读取:网站”

因此,我尝试为我的 Ubuntu LAMP 服务器制作一个 .sh 脚本,以便更轻松地向 apache2 添加新网站,但是当我尝试执行时出现错误

: bad variable name read: website

我搜索了一番,但找不到我需要的答案,在我的脚本中,有几行与导致错误的行设置相同。以下是我认为导致错误的行上的设置

read -p 'What is the website title (no spaces): ' website

以下是整个脚本:

echo Starting website builder
read -p 'What is the website title (no spaces): ' website
echo Website title saved as $website
echo creating storage folder
mkdir -p /var/www/$website
echo changing file permissions
chown -R $name:$name /var/www/$website
chmod -R 755 /var/www/$website
echo creating test file
echo <html> >> /var/www/$website/index.html
echo <head> >> /var/www/$website/index.html
echo    <title>Success</title> >> /var/www/$website/index.html
echo </head> >> /var/www/$website/index.html
echo <body> >> /var/www/$website/index.html
echo    <h1>$website is operational</h1> >> var/www/$website/index.html
echo </body> >> /var/www/$website/index.html
echo </html> >> /var/www/$website/index.html
clear
cat /var/www/$website/index.html
echo
read -p "press [Enter] to continue"
clear
echo creating $website.conf file
echo <VirtualHost *:80> >> /etc/apache2/sites-available/$website.conf
read -p 'Enter ServerName: ' ServerName
echo        ServerName $ServerName >> /etc/apache2/sites-available/$website.conf
read -p 'Enter ServerAlias (domain name): ' ServerAlias
echo        ServerAlias $ServerAlias >> /etc/apache2/sites-available/$website.conf
read -p 'Enter ServerAdmin: ' ServerAdmin
echo        ServerAdmin $ServerAdmin >> /etc/apache2/sites-available/$website.conf
echo        DocumentRoot var/www/$website >> /etc/apache2/sites-available/$website.conf
echo        ErrorLog ${APACHE_LOG_DIR}/error.log >> /etc/apache2/sites-available/$website.conf
echo        CustomLog ${APACHE_LOG_DIR}/access.log combined >> /etc/apache2/sites-available/$website.conf
echo </VirtualHost>
clear
cat /etc/apache2/sites-available/$website.conf
echo 
read -p "press [Enter] to continue"
clear
echo activating website
a2ensite $website
service apache2 restart
echo Open a web browser on any pc and visit $ServerAlias. If you have errors make sure the alias and external ip was properly set up on the domainservice you signed up with.
read -p 'Process completed. Press [Enter] to end.'
clear

答案1

乱码错误消息表明 Windows 行尾存在问题。您可以使用以下命令将文件中的行尾转换为换行符(Unix 格式):

sed -i 's/\r$//' /path/to/your/script

或者(您可能需要安装dos2unix 安装 dos2unix第一的):

dos2unix /path/to/your/script

进一步阅读:

相关内容