LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2014, 08:44 AM   #1
ampapa
LQ Newbie
 
Registered: Feb 2012
Posts: 28

Rep: Reputation: Disabled
Bash script to insert into SQLPlus


I'm really new to Bash and want to run a script that will write some records to a DB for disk usage and the like but am having a problem with the syntax.

I have the insert statements but am struggling on getting them to execute correctly in SQLPlus, can anyone assist?

Do I need to pass a variable to SQLPlus with the insert statements or can I call them directly?


Code:
#! /usr/bin/sh
userpass=pass

sqlplus -l myUSER/$userpass@myDB <<EOF
 df -Pk | awk 'NR > 1 {
  $1 = $1; printf "insert into STATS values ( \47%s\47,", h
  for (i = 0; ++i <=NF;)
    printf "\47%s\47,", $i
  print " null, null, null, \47df\47, current_timestamp );"
  }' OFS=, h="$(hostname)"
exit;
EOF
Thanks for any assistance.
 
Old 11-20-2014, 09:12 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,553

Rep: Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946
Quote:
Originally Posted by ampapa View Post
I'm really new to Bash and want to run a script that will write some records to a DB for disk usage and the like but am having a problem with the syntax.

I have the insert statements but am struggling on getting them to execute correctly in SQLPlus, can anyone assist? Do I need to pass a variable to SQLPlus with the insert statements or can I call them directly?
Code:
#! /usr/bin/sh
userpass=pass

sqlplus -l myUSER/$userpass@myDB <<EOF
 df -Pk | awk 'NR > 1 {
  $1 = $1; printf "insert into STATS values ( \47%s\47,", h
  for (i = 0; ++i <=NF;)
    printf "\47%s\47,", $i
  print " null, null, null, \47df\47, current_timestamp );"
  }' OFS=, h="$(hostname)"
exit;
EOF
Thanks for any assistance.
There was a previous thread on this site that had some examples on how to do just this:
http://www.linuxquestions.org/questi...tabase-666785/

This is the relevant part of a script I did some time ago to do this as well. Replace things as needed.
Code:
[ -f "dir.txt" ] && echo "File present" || exit 1
while read line
do
      echo "whatever data insert statement(s) you want here" >> /path/to/insert.sql
done < dir.txt
 
sqlplus -s username/password <<EOF
set scan off;
@/path/to/insert.sql
exit;
EOF
Essentially, I used the bash script to play with the variables accordingly, and build a sql file, then just called it via sqlplus. For me, it was much simpler to do it that way, since the bash portion was pure bash, and I just used the sqlplus statement to shovel through the .sql file built FROM the bash script.
 
Old 11-20-2014, 09:30 AM   #3
ampapa
LQ Newbie
 
Registered: Feb 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Much appreciated! I'll dig in and post back if I'm still having problems, thanks.

ampapa,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] mysql insert issue in bash script kamran.ayub Linux - Software 7 10-31-2014 02:53 PM
[SOLVED] mysql insert to table bash script tripialos Linux - General 19 05-15-2014 04:06 PM
How to insert header once in bash script manya Programming 1 08-24-2009 04:58 AM
bash script to insert id3 tags from file and directories jason7 Linux - General 15 07-20-2009 05:10 AM
mysql insert using bash script venki Linux - General 3 07-07-2007 04:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 PM.

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