说到 Linux,我是个新手。
我们的老师指派我们创建一个脚本来安装 LAMP 服务器,并可以选择安装不同的软件包(例如仅 MYSQL、PHP 或所有软件包)。
这是代码,通过后修复https://shellcheck.net/,
#!/bin/bash
function apache {
sudo apt update
sudo apt install apache2
#ja deaktivizē apache2 serveri un izmetas errors, kad mēģina to aktivizēt:
#sudo echo "deb http://archive.ubuntu.com/ubuntu trusty main universe restricted multiverse" > /etc/apt/sources.list
#sudo apt-get update
#sudo apt-get purge apache2*
#sudo apt-get install apache2
#sudo service apache2 start
#sudo service apache2 status #Pārbaudīt vai viss strādā pareizi
sudo ufw app list
sudo ufw allow in "Apache" #Šis profils atver tikai 80. portu (parasta, nešifrēta tīmekļa trafika).
sudo ufw enable #aktivizē ugunsmūri
sudo ufw status #pārbauda vai 80 portam ir atļauts iziet caur ugunsmūri
echo -e "Lai pārbaudītu vai viss sanāca, ieejiet jūsu pārlūkprogramma un ierakstiet šo http://jusu_servera_IP, Ip adresi var iegūt ar komandu hostname -I, tālāk ir dota jūsu IP adrese:"
hostname -I #parādīs IP adresi(pirmā)
echo "Ievadiet to konsolē, lai to varētu izmantot vēlāk" #write it in console to use it later
read -r ip_address
echo "Ievadi savu domēna vārdu" #input your domain name
read -r domain
sudo mkdir /var/www/"$domain"
sudo chown -R "$USER":"$USER" /var/www/"$domain"
cd /etc/apache2/sites-available/
echo "<VirtualHost *:80>
ServerName $domain
ServerAlias www.$domain
ServerAdmin webmaster@localhost
DocumentRoot /var/www/$domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>" > "$domain".conf
echo "Direktorija $domain un fails $domain.conf ir izveidoti"
sudo a2ensite "$domain"
sudo a2dissite 000-default
sudo systemctl reload apache2
sudo apache2ctl configtest
sudo systemctl reload apache2
cd /var/www/"$domain"
echo "<html>
<head>
<title>$domain majaslapa</title>
</head>
<body>
<h1>Sveiki pasaule!</h1>
<p>Si ir galvena lapa jusu majaslapai <strong>$domain</strong>.</p>
</body>
</html>" > index.html
#xdg-open http://localhost #lietojot operu met brīdinājumus
echo "Ierakstiet pārlūkprogramma http://localhost vai http://$ip_address"
}
function remove_apache {
#sudo service apache2 stop
sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
sudo apt-get autoremove --purge
echo -e "Vai jūs gribat arī noņemt apache izveidotās direktorijas (ja/ne) ?"
read -r manual
while [[ "$manual" != "ja" && "$manual" != "ne" ]]; do #salidzina && = AND
echo "Jums jaievada ja vai ne, lai turpinātu."
read -r manual
done
if [ "$manual" = ja ]; then
echo "Lai izdzēstu direktorijas, kuras jūs gribat izdzēst ar komandu sudo rm -Rf (piemēram, sudo rm -Rf /etc/apache2 /usr/lib/apache 2), tālāk būs dotas jūsu direktorijas:(ja tomēr nevēlaties izdzēst šīs direktorijas, ierakstiet jebko citu)"
whereis apache2
read -r remove_direktorijas
$remove_direktorijas
elif [ "$manual" = ne ]; then
echo -e "Apache2 ir atinstalēta."
fi
}
function mysql {
sudo apt install mysql-server
echo -e "Tālāk varēs izvēlēties vai instalēt mysql drošību ar VALIDATE PASSWORD PLUGIN vai bez(spiežot jebkuru citu pogu izņemot y vai Y)"
echo -e "Tālāk prasīs MYsql administrātora paroli vienalga vai jūs VALIDATE PASSWORD PLUGIN instalējāt vai nē. Un citas lietas, kas būs aprakstītas tālāk."
sudo mysql_secure_installation
echo -e "Lai izietu no MySQL konsoles rakstiet exit"
sudo mysql
}
function remove_mysql {
sudo apt-get remove --purge mysql*
}
#function php {
#sudo apt install php libapache2-mod-php php-mysql | man ieķērās instalācija(loop) ja liku funkcijā šo
#sudo systemctl restart apache2
#php -v
#}
function remove_php {
sudo apt-get purge php7.*
sudo apt-get autoclean
sudo apt-get autoremove
}
function phpmyadmin {
sudo apt update
echo -e "Parādīsies uzvedne, izvelāties apache2(jānospiež taustiņš space un enter)"
echo -e "Kad tiek vaicāts, vai datu bāzes iestatīšanai jāizmanto dbconfig-common, atlasiet Jā"
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
sudo phpenmod mbstring
sudo systemctl restart apache2
#xdg-open http://localhost/phpmyadmin
echo "Ierakstiet pārlūkprogramma http://localhost/phpmyadmin vai http://$ip_address/phpmyadmin"
}
function remove_phpmyadmin {
sudo apt-get remove phpmyadmin
sudo apt-get purge phpmyadmin
sudo apt-get autoremove
}
while true
do
echo "1. Instalēt Apache"
echo "2. Instalēt MySQL"
echo "3. Instalēt PHP"
echo "4. Instalēt phpMyAdmin"
echo "5. Instalēt LAMP(Visu)"
echo "6. Atinstalēt Apache"
echo "7. Atnstalēt MySQL"
echo "8. Atnstalēt PHP"
echo "9. Atinstalēt phpMyAdmin"
echo "10. Atinstalēt LAMP(Visu)"
echo "0. Iziet"
read -r INPUT
if [ "$INPUT" -eq 1 ]; then
apache
elif [ "$INPUT" -eq 2 ]; then
mysql
elif [ "$INPUT" -eq 3 ]; then
php
elif [ "$INPUT" -eq 4 ]; then
phpmyadmin
elif [ "$INPUT" -eq 5 ]; then
apache
mysql
sudo apt install php libapache2-mod-php php-mysql
sudo systemctl restart apache2
php -v
phpmyadmin
elif [ "$INPUT" -eq 6 ]; then
remove_apache
elif [ "$INPUT" -eq 7 ]; then
remove_mysql
elif [ "$INPUT" -eq 8 ]; then
remove_php
elif [ "$INPUT" -eq 9 ]; then
remove_phpmyadmin
elif [ "$INPUT" -eq 10 ]; then
remove_apache
remove_mysql
remove_php
remove_phpmyadmin
elif [ "$INPUT" -eq 0 ]; then
exit
fi
done
我该如何解决这个问题,因为错误是创建填满我的根分区的系统日志文件(shellcheck.net 中未提及)?
系统日志消息:
https://i.stack.imgur.com/bjVzH.jpg
这些消息只是说明此类行的行所需的整数表达式:
elif [ "$INPUT" -eq 5 ]; then
如果我echo "$INPUT"
按照 AdminBee 的建议在“read”之后插入,并在运行脚本后在终端中按 Enter 键,我会得到以下内容:
./lamp: line 161: [: : integer expression expected
./lamp: line 163: [: : integer expression expected
./lamp: line 165: [: : integer expression expected
./lamp: line 167: [: : integer expression expected
./lamp: line 169: [: : integer expression expected
./lamp: line 176: [: : integer expression expected
./lamp: line 178: [: : integer expression expected
./lamp: line 180: [: : integer expression expected
./lamp: line 182: [: : integer expression expected
./lamp: line 184: [: : integer expression expected
./lamp: line 189: [: : integer expression expected
答案1
问题在于这样的测试:
if [ "$INPUT" -eq 1 ]; then
由于测试整数相等性,如果为空(或者不是有效整数),则-eq
失败。$INPUT
在这种情况下,我们实际上不需要将其作为整数进行测试(例如,我们不需要将“01”“+1”和“1”视为全部相等),使用字符串更安全与 比较=
:
if [ "$INPUT" = 1 ]; then
(如果需要,您可以双引号1
,但这不是必需的。)顺便说一句,我还建议else
在所有测试之后添加一个子句:
else
echo "Unrecognized command: $INPUT"
# Maybe exit here?
fi
(当然,需要适当的翻译。)另一种选择是使用语句case
而不是一系列的if
/ elif
/etc:
case "$INPUT" in
1) apache ;;
2) mysql ;;
...
0) exit ;;
*) echo "Unrecognized command: $INPUT"
# Maybe exit here?
;;
esac
最好在脚本中添加一些错误检查。默认情况下,如果 shell 脚本中的某个命令失败,脚本的其余部分将盲目地继续执行,就好像没有发生任何错误一样。以本节为例:
cd /etc/apache2/sites-available/
echo "<VirtualHost *:80>
...
</VirtualHost>" > "$domain".conf
如果该cd
命令因任何原因失败,它将echo
继续并在您当时所在的任何目录中创建一个 .conf 文件。添加-e
到 shebang 行可能是最简单的事情:
#!/bin/bash -e
如果任何命令(测试中的命令或类似命令除外)失败,它会告诉 bash 退出。不幸的是,它不是很聪明;有时它由于看起来根本不应该是错误的事情而退出, 或者由于明显的原因而无法退出是错误。要详细解释为什么很难做到这一点,请参阅BashFAQ #105:为什么set -e
(或set -o errexit
, 或trap ERR
) 没有达到我的预期?
单个命令的错误检查确实更好,但工作量更大。
其他一些半风格的建议:使用小写或混合大小写的变量名(input
而不是INPUT
) 以避免意外使用具有特殊含义的全大写名称之一;避免echo -e
,因为某些版本只会打印“-e”作为输出的一部分(如果需要特殊处理,请使用printf
代替echo
);并缩进代码(例如内部函数定义、循环、条件等)以提高可读性。