Visit Our UNIX and Linux User Community


How to run bash script from within MySQL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run bash script from within MySQL
# 1  
Old 02-15-2011
How to run bash script from within MySQL

Can someone tell me the syntax to run a shell script from within MySQL? I know there is a way to do it, but I can't remember the syntax. I thought it was something like:

mysql> \. /user/myscript;

but I get this error:

Failed to open file '/user/myscript;', error: 2

Please help!
Thanks,
Peter V.
# 2  
Old 02-15-2011
Code:
mysql> \! /user/myscript

# 3  
Old 02-15-2011
verdepolo, thanks for the reply. It seems to be working, but now I'm getting this message:

my script contains one line:
Code:
 'show tables';

I've tried it unquoted and double quoted too.
When I try to run it, I get:
Code:
mysql> \! t2.sql;

/Users/peterv/bin/t2.sql: line 1: show tables: command not found

Running 'show tables' itself does work:
Code:
mysql> show tables;
+--------------------+
| Tables_in_project1 |
+--------------------+
| AUTHORBOOK         |
| myauthors          |
| mybooks            |
| test_books         |
+--------------------+
4 rows in set (0.00 sec)

mysql>


Any ideas what's causing my script to not work?
Thanks,
Peter V.
# 4  
Old 02-15-2011
I thought you were trying to run unix commands from a shell script but apparently what you are trying to do is run a set of mysql instructions from a file, correct?

Use this instead:

Code:
mysql> source /user/myscript;

# 5  
Old 02-15-2011
Sorry for the confusion, you are correct. Both of your replies have been added to my notes, and will are extremely useful. Thank you very much!

Previous Thread | Next Thread
Test Your Knowledge in Computers #684
Difficulty: Medium
According to both NetMarketShare and WikiMedia, Safari claimed a mobile market share of over 40% in 2019.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using bash script : How to Import data from a dsv file into multiple tables in mysql

HI I have a dsv file that looks like: <<BOF>> record_number|id_number|first name|last name|msisdn|network|points|card number|gender 312|9101011234011|Test Junior|Smith|071 123 4321|MTN|73|1241551413214444|M 313|9012023213011|Bob|Smith|27743334321|Vodacom|3|1231233232323244|M... (4 Replies)
Discussion started by: tera
4 Replies

2. Ubuntu

Bash script for FTP download -Mysql

Hi guys, I recently managed to write up my working script, but now I have a problem. If the file isn't there in the remote server, my actual script jumps it and all ok, but I need something like this: Search file -> if there, then download -> if not, download next file in the list. Any... (7 Replies)
Discussion started by: virtus96
7 Replies

3. Shell Programming and Scripting

Update a mysql column via bash script

Hello, I want to check the value of all MySQL columns.(column name is "status") via bash script. If value is "0" at I want to make only single column value to "1" I have many "0" values on mysql database(on "status" column) "0" means it is a draft post. I want to publish a post. I... (2 Replies)
Discussion started by: tara123
2 Replies

4. Shell Programming and Scripting

[BASH] Gawk + MYSQL script

Hello! I've got script to write. It should read databases (names, volumes) from table testdatabase and compares it to actually existing databases in /var/lib/mysql/. If there is no informations about database in table - we should see information "There is no declared informations about database... (1 Reply)
Discussion started by: Zimny
1 Replies

5. Shell Programming and Scripting

syntax issue mysql in bash script

I'm running mysql in a bash script mysql <<EOF query EOF one query is like this: UPDATE $dbname.$prefix"config" SET value = $var WHERE "$prefix"config.name = 'table colname'; with variable but it's giving an error i'm not sure what to put for "$prefix"config.name the table... (3 Replies)
Discussion started by: vanessafan99
3 Replies

6. Shell Programming and Scripting

syntax issue with quotes in mysql command for a bash script

i'm trying to write a bash script that executes a mysql statement mysql -sN -e INSERT INTO "$database"."$tableprefix"users (var1, var2,var3) VALUES (123, '1','') i don't know where to put the quotes it doesnt work with this one: ` it seems i can only put double quotes around the... (0 Replies)
Discussion started by: vanessafan99
0 Replies

7. Shell Programming and Scripting

ssh to run bash script

I want to use ssh to start a bash script that I have uploaded to a webhost. How do I do that from linux? (2 Replies)
Discussion started by: locoroco
2 Replies

8. Shell Programming and Scripting

Create mysql database with bash script - confused

Hi, i have the following: db="create database xxx;GRANT ALL PRIVILEGES ON xxx.* TO user@localhost IDENTIFIED BY 'password';FLUSH PRIVILEGES;quit;" mysql -u root -p$mysql_pass -e "$db" I don't understand why this is failing, it works fine when run from cmd but when is run in a bash script,... (1 Reply)
Discussion started by: ktm
1 Replies

9. Shell Programming and Scripting

Passing a MySql password from bash script

Hi all, I am running this script on Mandrakelinux release 10.1, 2.6.8.1-12mdksmp #1 SMP I have also installed 'expect' separately. I have created an Rsync script, but before any Rsync command does run, a MySql dump must be done first, and I am battling a bit to pass the MySql password from... (2 Replies)
Discussion started by: codenjanod
2 Replies

10. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

Featured Tech Videos