Um script que é executado bem a partir de quebras de arquivo quando executado com o Heredoc (na instalação do PHPmyadmin)

3

Eu posso executar o seguinte script usando a maneira tradicional de:

cd ~ && vi script.sh [PASTE SCRIPT INSIDE] && chmod +x script.sh && ./script.sh && rm -rf script.sh

No entanto, se eu executar exatamente o mesmo script com Heredoc (para colar e executar diretamente do prompt do Bash), o script será executado apenas parcialmente e será interrompido antes do final.

Por exemplo, é o que acontece quando eu executo o seguinte script com Heredoc:

A interface de instalação do PHPmyadmin funde-se com a CLI, enquanto a própria CLI perde quase qualquer recurso além de documentar a entrada padrão:

Oscript(comomeuHeredoc):

bash<<'EOT0'#!/bin/bash-x#SetupLAMPenvironmentwithenabledmodrewrite:cd~apt-getinstalllamp-server^-ya2enmodrewritecat<<EOF>>/etc/apach2/apache2.conf<Directory/var/www/>OptionsIndexesFollowSymLinksAllowOverrideAllRequireallgranted</Directory>EOFserviceapache2restart#SetupWebminandsomedependencies:apt-getinstallunzipperllibnet-ssleay-perlopenssllibauthen-pam-perllibpam-runtimelibio-pty-perlapt-show-versionspython-ywgethttp://prdownloads.sourceforge.net/webadmin/webmin_1.810_all.debdpkg--installwebmin_1.810_all.debapt-get-finstallsed-i's/ssl=1/ssl=0/g'/etc/webmin/miniserv.conf/etc/init.d/webminrestart#Crontasks:echo-e"" && echo -e "
cd ~ && vi script.sh [PASTE SCRIPT INSIDE] && chmod +x script.sh && ./script.sh && rm -rf script.sh
7" # Choose Nano (2) and do ^x. USER=benqzq crontab -u $USER -l 2>/dev/null cat <<- 'EOF' 0 8 * * * tar -zcvf /home/USERNAME/backups/files/www-html-$(date +\%F-\%T-).tar.gz /var/www/html 0 8 * * * find /home/USERNAME/backups/files/* -mtime +30 -exec rm {} \; 0 8 * * * mysqldump -u root -PASSWORD --all-databases > /home/USERNAME/backups/mysql/alldb_backup.sql 1 8 * * * tar -zcvf /home/USERNAME/backups/mysql/alldb_backup-$(date +\%F-\%T-).sql.tar.gz /home/USERNAME/backups/mysql/alldb_backup.sql 2 8 * * * rm /home/USER/backups/mysql/alldb_backup.sql 2 8 * * * find /home/USERNAME/backups/mysql/* -mtime +30 -exec rm {} \; EOF crontab -e USER=root # Setup PMA: echo -e "" && echo -e "
bash << 'EOT0'

#!/bin/bash -x

# Setup LAMP environment with enabled mod rewrite:
cd ~
apt-get install lamp-server^ -y
a2enmod rewrite
cat << EOF >> /etc/apach2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
EOF
service apache2 restart

# Setup Webmin and some dependencies:
apt-get install unzip perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python -y
wget http://prdownloads.sourceforge.net/webadmin/webmin_1.810_all.deb
dpkg --install webmin_1.810_all.deb
apt-get -f install
sed -i 's/ssl=1/ssl=0/g' /etc/webmin/miniserv.conf
/etc/init.d/webmin restart

# Cron tasks:
echo -e "" && echo -e "%pre%7" # Choose Nano (2) and do ^x.
USER=benqzq
crontab -u $USER -l 2>/dev/null
cat <<- 'EOF'
0 8 * * *  tar -zcvf /home/USERNAME/backups/files/www-html-$(date +\%F-\%T-).tar.gz /var/www/html
0 8 * * *  find /home/USERNAME/backups/files/* -mtime +30 -exec rm {} \;

0 8 * * *  mysqldump -u root -PASSWORD --all-databases > /home/USERNAME/backups/mysql/alldb_backup.sql
1 8 * * *  tar -zcvf /home/USERNAME/backups/mysql/alldb_backup-$(date +\%F-\%T-).sql.tar.gz /home/USERNAME/backups/mysql/alldb_backup.sql
2 8 * * *  rm /home/USER/backups/mysql/alldb_backup.sql
2 8 * * *  find /home/USERNAME/backups/mysql/* -mtime +30 -exec rm {} \;
EOF
crontab -e
USER=root

# Setup PMA:
echo -e "" && echo -e "%pre%7"
apt-get install phpmyadmin php-mb\string php-gettext -y
phpenmod mcrypt && phpenmod mbstring
bash -c "echo 'Include /etc/phpmyadmin/apache.conf' >> /etc/apache2/apache2.conf"
systemctl reload apache2.service

# Setup Fail2Ban:
apt-get install fail2ban -y

# Secure PMA HTTP authentication from BFAs with Fail2Ban:
cat << EOF > /etc/fail2ban/filter.d/phpmyadmin.conf
[Definition]
denied = mysql-denied|allow-denied|root-denied|empty-denied
failregex = ^<HOST> -.*(?:%(denied)s)$
ignoreregex =
EOF
cat << EOF >> /etc/fail2ban/jail.local
[phpmyadmin]
enabled = true
port = http,https
filter = phpmyadmin
logpath = /var/log/apache2/phpmyadmin_access.log
EOF

service  apache2 reload
service fail2ban reload
reboot

# Comment1
# Comment2
# Comment3
# ...

EOT0
7" apt-get install phpmyadmin php-mb\string php-gettext -y phpenmod mcrypt && phpenmod mbstring bash -c "echo 'Include /etc/phpmyadmin/apache.conf' >> /etc/apache2/apache2.conf" systemctl reload apache2.service # Setup Fail2Ban: apt-get install fail2ban -y # Secure PMA HTTP authentication from BFAs with Fail2Ban: cat << EOF > /etc/fail2ban/filter.d/phpmyadmin.conf [Definition] denied = mysql-denied|allow-denied|root-denied|empty-denied failregex = ^<HOST> -.*(?:%(denied)s)$ ignoreregex = EOF cat << EOF >> /etc/fail2ban/jail.local [phpmyadmin] enabled = true port = http,https filter = phpmyadmin logpath = /var/log/apache2/phpmyadmin_access.log EOF service apache2 reload service fail2ban reload reboot # Comment1 # Comment2 # Comment3 # ... EOT0

Atualização para Stéphane Chazelas

    
por JohnDoea 16.11.2016 / 20:50

2 respostas

3

Você está fazendo:

bash << 'EOT'
some-command-that-reads-stdin
EOT

Mas o stdin de some-command-that-reads-stdin também será esse documento, pois ele é iniciado por bash , portanto, herda o mesmo stdin .

Você pode fazer:

bash /dev/fd/3 3<< 'EOT'
some-command-that-reads-stdin
EOT

Então stdin é deixado intacto e bash obtém o código desse documento aqui em outro fd.

    
por 16.11.2016 / 22:26
0

Provavelmente está usando o shell errado. Eu removeria a linha vazia entre o bash << 'EOT0' e o #!/bin/bash -x .

Você pode adicionar essa linha para verificar qual shell é usado:

echo $SHELL >/tmp/shell.txt

Em seguida, verifique o resultado com um simples gato:

cat /tmp/shell.txt
    
por 16.11.2016 / 21:14