1

So I have this #!/bin/bash script that installs some stuff, and then at the end of the script I want it to run the next script in line.

For instance, Script 1 installs LAMP, but at the end of that script I want it to run a different (also #!/bin/bash) script that runs moodle, for instance.

I tried just putting the sudo ./Mysql#2 (my next script) at the end but I get an 'error redirection' unexpected when I the script reaches that part when running. FYI, MYSQL#2 is in the same directory. All scripts I am trying to chain are in the same directory.

First 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  

Second 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 

How do I get around this? I don't want any interruptions between each script as they run, I want it sequentially without having to touch the keyboard.

  • I think you will have to tell us what's in the script(s) and some more about the environment (nextscriptname is in the same directory as the first script, right?) because it should work ok in theory – Zanna Apr 30 '17 at 19:58
  • 1
    You can call another Bash script just like any other command or executable from any place in your first script. There is absolutely nothing special to it. Probably your error is caused by something else, therefore it would be helpful if you could provide the full content of both of your scripts, as Zanna already said. – Byte Commander Apr 30 '17 at 20:27
  • @ByteCommander - added edits that add everything missing – nwd12a May 1 '17 at 18:12
  • 1
    Could you run your script in debug mode once (bash -x SCRIPTFILE)? That way it will print each command line before it gets executed and you can exactly locate at which line the error occurs. – Byte Commander May 1 '17 at 19:48
1

It is straightforward to call a shellscript from another shellscript, if all scripts are in the current directory and they have execute permissions.

See the following example.

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

Command and output, when run as the user '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 ***
0

Instead of running the second script from the first script like so:

sudo ./Mysql#2

Try doing this:

sudo -s source ./Mysql#2

or if you're running the script as root, just run the second script like so:

source ./Mysql#2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.