Descartando outro script de dentro de um script

1

Portanto, eu tenho este script #!/bin/bash que instala algumas coisas e, no final do script, quero que ele execute o próximo script na linha.

Por exemplo, o Script 1 instala o LAMP, mas no final desse script eu quero que ele execute um script diferente (também #!/bin/bash ) que executa o moodle, por exemplo.

Eu tentei apenas colocar o sudo ./Mysql#2 (meu próximo script) no final, mas recebo um 'redirecionamento de erro' inesperado quando o script alcança essa parte ao executar. FYI, MYSQL # 2 está no mesmo diretório. Todos os scripts que estou tentando encadear estão no mesmo diretório.

Primeiro script:

#!/bin/bash 

DATE=$(date)

export DEBIAN_FRONTEND='noninteractive'

# Install apache and begin ssl setup 
apt-get -y install apache2
a2enmod ssl
service apache2 restart
mkdir /etc/apache2/ssl

echo '-------------------------------------------'
echo 'installed apache and created ssl directory.' 
echo '-------------------------------------------'

sleep 3 

echo ""
echo ""

echo ------------------------------------------------------------------------------------#
echo 'Now Generate SSL Key. Now we will use sed to adjust the values in default-ssl.conf.' 
echo ------------------------------------------------------------------------------------#

sleep 5 

#You can generate an openssl key non-interactively if you uncomment the line below. 
#My server already has run this command, so the key gen breaks, but if you running this command to grade it should work.
openssl genrsa -out /etc/apache2/ssl/apache.key 2048
openssl req -nodes -new -key /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.csr -subj "/C=US/ST=Texas/L=Abilene/O=ACU/OU=IT410/CN=69.28.90.132"



echo -----------------#
echo 'Changed values.' 
echo -----------------#

sleep 2 

echo ""
echo ""

a2ensite default-ssl.conf
service apache2 restart

echo ""
echo ""

echo --------------------------------------# 
echo 'Time to grab php7.0 and php7.0 mods.'
echo --------------------------------------#

sleep 3 


# install php mods + configure mailutils for only outgoing mail 
export DEBIAN_FRONTEND='noninteractive'
apt-get install -y php7.0 php7.0-mysql libapache2-mod-php7.0 php7.0-curl php-pear php-db php7.0-ldap mailutils  

echo "" 
echo "" 

echo '-----------------------------------'
echo 'installed php, php mods, and mail.' 
echo '-----------------------------------'

sleep 3 

echo ""
echo ""

echo --------------------------------------#
echo 'Setting inet_interfaces to localhost.'
echo --------------------------------------#

sleep 3 

sed -i 's@inet_interfaces.*@inet_interfaces = localhost@' /etc/postfix/main.cf

echo ""
echo ""

echo ------------------------#
echo 'Made mail outgoing only.'  
echo ------------------------#

service postfix restart

# Logging 

echo -e 'apache2 installed + ssl keys + default-ssl.conf -done by' $USER 'at time\n' $DATE '\n' >> /var/log/installs/log.txt
echo -e 'php7.0 + mods + mailutils + mail outgoing only -done by' $USER 'at time\n' $DATE '\n' >> /var/log/installs/log.txt
echo -e 'LAMP + Maldetect and Php mods+ mail complete by' $USER 'at time\n' $DATE '\n' >> /var/log/installs/log.txt

sudo ./Mysql#2  

Segundo script:

#!/bin/bash

#make sure you run script as sudo mysql-install.sh (That way, all commands within the script will be run with root privileges and you only need to give the password once 
#when launching the script. If you need a particular command within the script to be run without sudo privileges, you can run it as a regular user with: sudo -u username command 

######################
#                    #
#                    #
#       WORKS        #
#                    #
#                    #
######################

# mv FILES to EASIER NAMES /home/nwd12a

# chmod u+x FILES 


DATE=$(date) 
PW='KappaRoss\n' 
newuser='nwd12a' 

# Tell the user the script is starting
echo ------------------------------------------------#
echo "Install script for mysql+secure is now running." 
echo ------------------------------------------------# 

sleep 3 

echo ------------------------------------#
echo 'Removing mysql if it was installed.'
echo ------------------------------------# 

sleep 2 

#Purge MYSQL install if you have it 
sudo -S apt-get -y remove --purge mysql-server mysql-client mysql-common
apt-get -y autoremove 
sudo -S rm -rf /var/lib/mysql 

# Update First 
apt-get -q update 

#Install MYSQL and run secure installation from script (debconf allows you to seed ahead of time)
debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password password KappaRoss'
debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password_again password KappaRoss'
apt-get -y install mysql-server

# Modify my.cnf file to have your mysql user name and password saved 

chmod 777 /etc/mysql/my.cnf
echo -e '[client]\nuser = root\npassword = KappaRoss' >> /etc/mysql/my.cnf
chmod 400 /etc/mysql/my.cnf 

echo -------------------------------------# 
echo 'Configuring mysql database securely.'
echo -------------------------------------# 

sleep 2  

#MySQL secure installation 
mysql -e "SET PASSWORD for 'root'@'localhost' = PASSWORD('KappaRoss');" 
mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
mysql -e "DELETE FROM mysql.user WHERE User='';"  
mysql -e "DROP DATABASE IF EXISTS test;"   
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%';" 
mysql -e "FLUSH PRIVILEGES;" 

echo ---------------------------------# 
echo 'Opening port 3306, just in case.'
echo ---------------------------------# 

sleep 2 

#check to make sure port 3306 is open - port will be opened for input and output after this command 
iptables -A INPUT -p tcp -s 15.15.15.0/24 --dport 3306 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 3306 -m conntrack --ctstate ESTABLISHED -j ACCEPT

echo -------------------------------------------------------# 
echo 'Creating table, user and privileges in mysql database.'
echo -------------------------------------------------------# 

sleep 3 

#open mysql, create new user, create table (mysql non-interactive)   
mysql -e "use mysql;"  
mysql -e "create user 'nwd12a'@'localhost' identified by 'Newvegas3';" 
mysql -e "show databases;"  

echo --------------------------------# 
echo 'Shown to have no test database.'
echo --------------------------------# 

sleep 2 

mysql -e "create database it_410;"  
mysql -e "show databases"
mysql -e "GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER on it_410.* to 'nwd12a'@'localhost' identified by 'Newvegas3';"
mysql -e "flush privileges;"  

# create a user in mysql that has remote permissions (for mysqlworkbench) 
sed -i 's/bind-address.*/bind address = 0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf

mysql -e "CREATE USER 'IT410'@'%' IDENTIFIED BY 'final';"
mysql -e "GRANT ALL ON *.* TO 'IT410'@'%';"
mysql -e "flush privileges;"

# Tell the user the script is finished 
echo ----------------------------------#
echo "Install Script has finished" 
echo ----------------------------------#

# Logging to /var/log/installs/log.txt 

echo -e 'mysql secure install was started -done by' $USER 'at time\n' $DATE '\n' >> /var/log/installs/log.txt
echo -e 'if mysql installed, it was purged -done by' $USER 'at time\n' $DATE '\n' >> /var/log/installs/log.txt
echo -e 'mysql has been installed -done by' $USER 'at time\n' $DATE '\n' >> /var/log/installs/log.txt
echo -e 'port 3306 opened -done by' $USER 'at time\n' $DATE '\n' >> /var/log/installs/log.txt
echo -e 'new user nwd12a + table it-410 made -done by' $USER 'at time\n' '\n' $DATE >> /var/log/installs/log.txt
echo -e 'mysql secure install completed by' $USER 'at time\n' $DATE '\n' >> /var/log/installs/log.txt 

Como faço para contornar isso? Não quero interrupções entre cada script enquanto eles são executados, quero sequencialmente sem precisar tocar no teclado.

    
por nwd12a 30.04.2017 / 21:52

2 respostas

1

É simples chamar um shellscript de outro shellscript, se todos os scripts estiverem no diretório atual e tiverem permissões de execução.

Veja o exemplo a seguir.

Script master :

#!/bin/bash

echo "*** start master ***

assumption:  all scripts are in the current directory
             and they have execute permissions.

alternative: create the directory 'bin' in your home directory and move
             your scripts to there. Then (in any new terminal window)
             your scripts will be in PATH and can be called without any
             explicit path"

./sub1
./sub2

sudo ./sub1   # run with sudo should work too

echo "*** finish master ***"

Script sub1 :

#!/bin/bash

echo "*** start sub1 ***"

whoami

echo "*** finish sub1 ***"

Script sub2 :

#!/bin/bash

echo "*** sub2 ***"

Comando e saída, quando executado como usuário 'sudodus'

$ ./master
*** start master ***

assumption:  all scripts are in the current directory
             and they have execute permissions.

alternative: create the directory 'bin' in your home directory and move
             your scripts to there. Then (in any new terminal window)
             your scripts will be in PATH and can be called without any
             explicit path
*** start sub1 ***
sudodus
*** finish sub1 ***
*** sub2 ***
[sudo] password for sudodus: 
*** start sub1 ***
root
*** finish sub1 ***
*** finish master ***
    
por sudodus 07.05.2017 / 10:06
0

Em vez de executar o segundo script do primeiro script da seguinte forma:

sudo ./Mysql#2

Tente fazer isso:

sudo -s source ./Mysql#2

ou se você estiver executando o script como root, basta executar o segundo script da seguinte forma:

source ./Mysql#2
    
por Nicholas Neal 07.05.2017 / 08:50