所以我有这个#!/bin/bash
安装一些东西的脚本,然后在脚本的末尾我希望它运行下一个脚本。
例如,脚本 1 安装了 LAMP,但在该脚本的末尾,我希望它运行一个不同的#!/bin/bash
脚本(例如,运行 moodle)。
我尝试将 sudo ./Mysql#2(我的下一个脚本)放在最后,但当脚本运行时到达该部分时,我意外地收到“错误重定向”提示。仅供参考,MYSQL#2 位于同一目录中。我尝试链接的所有脚本都位于同一目录中。
第一个脚本:
#!/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
第二个脚本:
#!/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
我该如何解决这个问题?我不希望每个脚本运行时出现任何中断,我希望按顺序运行它们而无需触摸键盘。
答案1
如果所有脚本都在当前目录中并且具有执行权限,则可以直接从另一个 shell 脚本调用一个 shell 脚本。
请参阅以下示例。
脚本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 ***"
脚本sub1
:
#!/bin/bash
echo "*** start sub1 ***"
whoami
echo "*** finish sub1 ***"
脚本sub2
:
#!/bin/bash
echo "*** sub2 ***"
以用户“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 ***
答案2
而不是像这样从第一个脚本运行第二个脚本:
sudo ./Mysql#2
尝试这样做:
sudo -s source ./Mysql#2
或者如果你以 root 身份运行脚本,则只需像这样运行第二个脚本:
source ./Mysql#2