LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-17-2022, 01:59 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
Question What exactly is causing shell script to fail?


Hey,

Automation MySQL secure installation by a shell script.

Platform: AWS Cloud
Instance Type: t2.micro

MySQL Edition: MySQL Community Edition 5.8

What work this far beautifully is:

Code:
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm -y
sudo amazon-linux-extras install epel -y
sudo yum -y install mysql-community-server
sudo systemctl enable mysqld
sudo systemctl start mysqld
sudo systemctl status mysqld 
export CURRENT_MYSQL_PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log| awk '{print $NF}')
echo ${CURRENT_MYSQL_PASSWORD}
export NEW_MYSQL_PASSWORD='NS!rQdzhF44^y2VEiI'
Now comes the step of MySQL secure installation which is where I am issue.

Script:
Code:
#!/bin/bash

set -x
if [ -n "${1}" -a -z "${2}" ]; then
    # Setup root password
    CURRENT_MYSQL_PASSWORD=''
    NEW_MYSQL_PASSWORD="${1}"
elif [ -n "${1}" -a -n "${2}" ]; then
    # Change existens root password
    CURRENT_MYSQL_PASSWORD="${1}"
    NEW_MYSQL_PASSWORD="${2}"
else
    echo "Usage:"
    echo "  Setup mysql root password: ${0} 'your_new_root_password'"
    echo "  Change mysql root password: ${0} 'your_old_root_password' 'your_new_root_password'"
    exit 1
fi

SECURE_MYSQL=$(expect -c "

set timeout 3
spawn mysql_secure_installation

expect \Enter password:\"
send \"${CURRENT_MYSQL_PASSWORD}\r\"

expect \New password:\"
send \"${NEW_MYSQL_PASSWORD}\r\"

expect \Re-enter new password:\"
send \"${NEW_MYSQL_PASSWORD}\r\"

expect \"Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :\"
send \"y\r\"

expect \"Remove anonymous users? (Press y|Y for Yes, any other key for No) :\"
send \"y\r\"


expect \"Disallow root login remotely? (Press y|Y for Yes, any other key for No) :\"
send \"y\r\"


expect \"Remove test database and access to it? (Press y|Y for Yes, any other key for No) :\"
send \"y\r\"

expect \"Reload privilege tables now? (Press y|Y for Yes, any other key for No) :\"
send \"y\r\"

expect eof
")

echo "${SECURE_MYSQL}"

exit 0
However, when I start executing, I get an error:

Code:
[ec2-user@ip-172-31-40-83 ~]$ ./dbsetup.sh ${CURRENT_MYSQL_PASSWORD} ${NEW_MYSQL_PASSWORD}
+ PURGE_EXPECT_WHEN_DONE=0
+ '[' -n '5Wg9&WLGzAe6' -a -z 'NS!rQdzhF44^y2VEiI' ']'
+ '[' -n '5Wg9&WLGzAe6' -a -n 'NS!rQdzhF44^y2VEiI' ']'
+ CURRENT_MYSQL_PASSWORD='5Wg9&WLGzAe6'
+ NEW_MYSQL_PASSWORD='NS!rQdzhF44^y2VEiI'
++ expect -c '

set timeout 3
spawn mysql_secure_installation

expect \Enter password:"
send "5Wg9&WLGzAe6\r"

expect \New password:"
send "NS!rQdzhF44^y2VEiI\r"

expect \Re-enter new password:"
send "NS!rQdzhF44^y2VEiI\r"

expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
send "y\r"

expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "y\r"


expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
send "y\r"


expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
send "y\r"

expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
send "y\r"

expect eof
'
invalid command name "password:""
    while executing
"password:""
    invoked from within
"expect \Enter password:""
+ SECURE_MYSQL='spawn mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: '
+ echo 'spawn mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: '
spawn mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:
+ '[' 0 -eq 1 ']'
+ exit 0
[ec2-user@ip-172-31-40-83 ~]$ mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Last edited by sysmicuser; 07-17-2022 at 02:00 AM. Reason: Additional Information.
 
Old 07-17-2022, 02:22 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,305
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
It looks like you might be confusing shells scripts with Expect. Expect is a different language based on TCL and thus would need a different interpreter.
 
Old 07-17-2022, 05:08 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by sysmicuser View Post
Code:
[ec2-user@ip-172-31-40-83 ~]$ ./dbsetup.sh ${CURRENT_MYSQL_PASSWORD} ${NEW_MYSQL_PASSWORD}
I don't know the answer, but you must use quotation everywhere, like this:
Code:
[ec2-user@ip-172-31-40-83 ~]$ ./dbsetup.sh "${CURRENT_MYSQL_PASSWORD}" "${NEW_MYSQL_PASSWORD}"
Also you can use shellcheck to check your script
 
Old 07-17-2022, 07:24 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,691

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
shellcheck can not check expect syntax.

It is possible to write the entire script using tcl/expect. I don't think I have ever tried running debug (set -x) with expect so I do not know if the output reflects exactly what is happening.

Quote:
expect \Enter password:"
It looks like you are missing a ". In addition it looks like the mysql_secure_installation is asking "Enter password for user root: " but you are only looking for "Enter password"

You can try using autoexpect to generate the expect part of the script and then modify it as desired.
 
Old 07-17-2022, 11:59 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Code:
expect \Enter password:\"
Missing the opening quote (three times)?
Code:
expect \"Enter password:\"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] qcow2 image file causing guest install to fail due to insufficient space maccas17 Linux - Virtualization and Cloud 14 11-01-2018 07:37 AM
[SOLVED] if [[ -n "$1" ]]; then FAIL FAIL FAIL rbees Programming 7 03-25-2015 02:39 PM
Fedora C3 SELinux causing Free-Radius to fail startup aflack Linux - Security 2 08-09-2007 11:01 AM
MKDIR trying to use version 2.4 of libc, not 2.3.6 and causing step 6.5 to fail MrMark Linux From Scratch 1 01-30-2007 07:32 PM
Module conflict causing network services to fail rollo Linux - Newbie 3 11-01-2005 12:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration