[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1474
  • Last Modified:

Inserting data into mysql database from bash script?

I have a textfile, a portion of the data is going into mysql.
I could write a separate program to handle this and run it from the shell script, but it would be nice if I could get it to work straight from bash. (If only to learn more bash)
I'm way of my bash league here, but I imagine something like
`mysql -h host -u user -p pass < insert into x (foo, bar) values ("baz", "$(script to get data)") `
The data I'm looking for will have ------- both prepended and appended.
0
letharion
Asked:
letharion
  • 3
1 Solution
 
hernst42Commented:
You cpuld try the following and make sure the script escapes the values suitable for the insert (e.g ' is not returned by the script.
echo "insert into x (foo, bar) values ('baz', '"$(script to get data)"');" | mysql -h host -u user --password=pass

Open in new window

0
 
letharionAuthor Commented:
This seems to work pretty well :)

echo 'use PPS; start transaction; insert into ProgramTestTasks (project, run, function_code) values ("2230", "1060LC", "'"$SYSTEM"'"); select max(pkey) from ProgramTestTasks; commit;' > tmpQuery
cat tmpQuery
$Res=$(mysql -h 80.252.191.50 -u freas --password=freas < tmpQuery)
echo "$Res"

Copy pasting to mysql the exact command that cat prints works great, but  for some reason echo $RES doesn't return anything useful, but rather
line 44: =max(pkey): command not found
As if it's expanding $RES to run it's content as commands.
I randomly tried using `` instead of $(..) but that didn't make any difference.
0
 
letharionAuthor Commented:
Ops,
$Res=
won't work. Doh
0
 
gtkfreakCommented:
Would you need quotes around it?
0
 
letharionAuthor Commented:
gtkfreak: Sorry, not sure what you mean?
Anyway, I got this working with hernst42's solution.
My mistake was to try assigning to $res, and assignment in bash should leave out the $
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now