1 votes

Lancer un autre script à l'intérieur d'un script.

J'ai donc ceci #!/bin/bash script qui installe quelques trucs, et ensuite à la fin du script je veux qu'il exécute le script suivant dans la ligne.

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

J'ai essayé de mettre simplement le sudo ./Mysql#2 (mon prochain script) à la fin mais j'obtiens une 'erreur de redirection' inattendue quand je le script atteint cette partie lors de l'exécution. Pour info, MYSQL#2 est dans le même répertoire. Tous les script que j'essaie d'enchaî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 ce problème ? Je ne veux pas d'interruption entre chaque script lors de leur exécution, je veux que ce soit séquentiel sans avoir à toucher le clavier.

1voto

sudodus Points 39902

Il est simple d'appeler un shellscript à partir d'un autre shellscript, si tous les scripts sont dans le répertoire courant et qu'ils ont les droits 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 ***

0voto

Brian Leeming Points 2292

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

sudo ./Mysql#2

Essayez de faire ça :

sudo -s source ./Mysql#2

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

source ./Mysql#2

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X