web-dev-qa-db-fra.com

Lancer un autre script à partir d'un script

J'ai donc ce #!/bin/bash script qui installe des trucs, puis à la fin du script je veux qu'il exécute le prochain script en ligne.

Par exemple, Script 1 installe LAMP, mais à la fin de ce script, je veux qu'il exécute un autre (également #!/bin/bash) script qui exécute moodle, par exemple.

J'ai essayé de simplement mettre le Sudo ./Mysql#2 (mon prochain script) à la fin mais j'obtiens une "redirection d'erreur" inattendue lorsque le script atteint cette partie lors de l'exécution. Pour info, MYSQL # 2 est dans le même répertoire. Tous les scripts que j'essaye de chaîner sont dans le même répertoire.

Premier 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  

Deuxième 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 

Comment puis-je contourner cela? Je ne veux pas d'interruptions entre chaque script pendant leur exécution, je le veux séquentiellement sans avoir à toucher le clavier.

1
nwd12a

Il est simple d'appeler un shellscript à partir d'un autre shellscript, si tous les scripts se trouvent dans le répertoire courant et qu'ils ont des autorisations d'exécution.

Voir l'exemple suivant.

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 ***"

Commande et sortie, lorsqu'elle est exécutée en tant qu'utilisateur '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 ***
1
sudodus

Au lieu d'exécuter le deuxième script à partir du premier script comme ceci:

Sudo ./Mysql#2

Essayez de faire ceci:

Sudo -s source ./Mysql#2

ou si vous exécutez le script en tant que root, exécutez simplement le deuxième script comme ceci:

source ./Mysql#2
0
Nicholas Neal