Score:1

Bash script pauses after opening mysql

br flag

I'm trying to write a script that will handle some maintenance on our Ubuntu server for the even-less Linux-savvy techs than myself.

When I run the script as administrator (needed to access mysql), it prompts me for the password correctly, the mysql command starts successfully, but the following commands do not run. The Terminal just sits there and doesn't continue or fail.

Any idea what I'm doing wrong would be very helpful. I've done some research but no solutions forthcoming and rather hard to Google.

#! /bin/sh
mysql -p fog

DELETE FROM `hosts` WHERE `hostID` = '0';
#more DELETE commands in here, removed for brevity
DELETE FROM tasks WHERE taskTypeId=8;

$Shell
Score:0
hr flag

The mysql command expects to read commands from standard input. You can provide this in a script using a here document:

#! /bin/sh

mysql -p fog <<'EOF'
DELETE FROM 'hosts' WHERE 'hostID' = '0';
#more DELETE commands in here, removed for brevity
DELETE FROM tasks WHERE taskTypeId=8;
EOF

Control will return to the shell once the mysql command exits. Quoting the first 'EOF' (or equally well \EOF or "EOF") prevents the shell from expanding the contents - not an issue for the commands in your example, but something like SELECT * FROM would otherwise expand the * to a list of files. The actual string EOF is arbitrary.

GBarron avatar
br flag
Thanks- I believe this answers my question. I can't actually tell for sure if it's working, though. When I use the here document as recommended, the Terminal closes almost immediately after entering my password. It *could* be working, but I'm not sure why the $shell at the end of the script isn't keeping the Terminal open. I mainly just want to make sure everything is running right.
GBarron avatar
br flag
I'm also reading more into the here documents as well- neat stuff. I'm essentially completely new to bash... I know the basics of navigating the Terminal, and I have some experience with PowerShell, but otherwise I'm completely new to bash.
hr flag
@GBarron I suggest running the script from an interactive shell until you have debugged it. If you *must* run it from some kind of launcher, then adding a `read` command should hold the terminal open until a key is pressed. If you want to hold it open by starting a new shell you'd likely need upper case `$SHELL` rather than `$shell` (which won't be set to anything, by default).
GBarron avatar
br flag
Thanks again- you're right, I had `$SHELL` wrong. The `<<EOF` also needed to be entered without the single quotes to work. I have a separate issue now- the command seems to run successfully, but the here document input is fired before my password is entered and maybe even before the mysql server is selected. I get a pile of output lines: `/home/username/Desktop/FOGQueueFix.sh: 1: hosts: not found` Edit: So basically, every variable in the input I sent not found.
GBarron avatar
br flag
Additionally, after I enter the password I get: ```ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE = '0'' at line 1 ``` However, I know this command works when simply entered in manually.
hr flag
@GBarron if you do not quote the `EOF` and use backquotes around `hostID` as in your original then the shell will treat it as a command substitution. Since there probably isn't a `hostID` command, the result will be empty resulting in incorrect syntax `WHERE = '0'`
GBarron avatar
br flag
I see- thanks again for your time. Put the back quotes back and fixed shell. The command stays open now. I don't see any output from mysql which is strange, but I believe it's all working now.
Score:0
in flag

Another to possibility is to create 2 files: one for your bash script (say mybash, the other one for the SQL commands (mydeletes.sql):

mybash

#! /bin/sh

mysql -p fog <<mydeletes.sql

mydeletes.sql

DELETE FROM 'hosts' WHERE 'hostID' = '0';
#more DELETE commands in here, removed for brevity
DELETE FROM tasks WHERE taskTypeId=8;

This will separate SQL code from bash code. It will allow you to load, execute and debug your SQL script within many (if not all) SQL tool (DBeaver, Workbench,...)

GBarron avatar
br flag
Thanks Marc, another good alternative answer!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.