Saturday, April 27, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 387  / 2 Years ago, fri, june 24, 2022, 11:39:38

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

More From » command-line

 Answers
5

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.


[#1396] Saturday, June 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nuehan

Total Points: 253
Total Questions: 109
Total Answers: 120

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
nuehan questions
Sun, Dec 18, 22, 17:34, 1 Year ago
Mon, Mar 28, 22, 00:10, 2 Years ago
Thu, Apr 28, 22, 20:54, 2 Years ago
Fri, Aug 20, 21, 01:06, 3 Years ago
Fri, Dec 9, 22, 15:02, 1 Year ago
;