0

I am newbie when it comes to Linux.

Our teacher assigned us to create a script which would install LAMP server with choices to install different packages(for example only MYSQL, or PHP, or everything).

Here is the code, fixed after having passed through 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

How can I fix this, as the error is creating syslog files that fill up my root partition(was not mentioned in shellcheck.net)?

syslog messages:

https://i.sstatic.net/bjVzH.jpg

The messages just say integer expression expected for lines where this type of line is:

elif [ "$INPUT" -eq 5 ]; then

If I insert echo "$INPUT" after the `read' as suggested by AdminBee and in terminal press enter after running the script I get these:

./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
4
  • What large number of messages are being written to the syslog files that are generated because of running this script? Can you provide us a few, please, as I don't see any useful link between the script and syslog. Commented Jan 5, 2021 at 14:43
  • If you insert and echo "$INPUT" after the read, what does it say? Commented Jan 5, 2021 at 16:23
  • Those error messages seem to indicate that $INPUT is blank. Are you actually entering a number when prompted? In any case, using = comparisons (string equality) might be better than -eq (numeric equality), since it won't panic if $INPUT is set to something other than an integer. Commented Jan 6, 2021 at 9:09
  • @GordonDavisson This worked, can you post it as answer? Commented Jan 10, 2021 at 1:46

1 Answer 1

0

The problem was with tests like this:

if [ "$INPUT" -eq 1 ]; then

Since -eq tests for integer equality, it was failing if $INPUT was blank (or otherwise not a valid integer). In this case, we don't really need to test it as an integer (for example, we don't need to treat "01" "+1" and "1" as all being equal), it's safer to use a string comparison with =:

if [ "$INPUT" = 1 ]; then

(You could double-quote the 1 if you wanted, but it's not necessary.) BTW, I'd also recommend adding an else clause after all of the tests:

else
    echo "Unrecognized command: $INPUT"
    # Maybe exit here?
fi

(with appropriate translation, of course.) Another option is to use a case statement instead of the series of if/elif/etc:

case "$INPUT" in
    1) apache ;;
    2) mysql ;;
    ...
    0) exit ;;
    *) echo "Unrecognized command: $INPUT"
        # Maybe exit here?
        ;;
esac

It'd also be good to add some error-checking to the script. By default, if a command in a shell script fails, the rest of the script will just blindly continue on as though nothing were wrong. Take this section:

cd /etc/apache2/sites-available/

echo "<VirtualHost *:80>
    ...
    </VirtualHost>" > "$domain".conf
    

If the cd command fails for any reason, the echo will go ahead and create a .conf file in whatever directory you happen to be in at the time. Probably the simplest thing to do as add -e to the shebang line:

#!/bin/bash -e

which tells bash to exit if any command (other than those in a test or something like that) fails. Unfortunately, it's not very smart; it sometimes exits due to things that don't really seem like they should be errors at all, or fails to exit due to things that clearly are errors. For a good explanation of why this is hard to get right, see BashFAQ #105: Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected?

Individual-command error checks are really better, but a lot more work.

A few other semi-stylistic recommendations: use lower- or mixed-case variable names (input instead of INPUT) to avoid accidentally using one of the all-caps names with a special meaning; avoid echo -e since some versions will just print "-e" as part of the output (if you need special processing, use printf instead of echo); and indent your code (e.g. inside function definitions, loops, conditionals, etc) for readability.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.