Solved

Shell Script

Posted on 2011-03-08
5
364 Views
Last Modified: 2012-05-11
Hi

I have a delete.dat and insert.dat files.  I want to run Shell script to update records in database

when i passwd three argument for deletion with Shell script as


./shellscript.sh delete tom delete.dat

it has to Create a sql file as below

delete from tabl1 where name = 'tom' and id = 1;
delete from tabl1 where name = 'tom' and id = 2;
delete from tabl1 where name = 'tom' and id = 3;
delete from tabl1 where name = 'tom' and id = 4;

delete.dat file will following data
1
2
3
4

when i passwd three argument for insert to same Shell script as

./shellscript.sh insert tom insert.dat

it has to Create a sql file as below

insert into tab1 values(id_sequence.nextval,'tom'1);
insert into tab1 values(id_sequence.nextval,'tom'2);
insert into tab1 values(id_sequence.nextval,'tom'3);
insert into tab1 values(id_sequence.nextval,'tom'3);

insert.dat file will following data
1
2
3
4

SQL file
0
Comment
Question by:vadicherla
  • 2
  • 2
5 Comments
 

Author Comment

by:vadicherla
ID: 35079547
Hi

I have a delete.dat and insert.dat files.  I want to run Shell script to update records in database

when i passwd three argument for deletion with Shell script as


./shellscript.sh delete tom delete.dat

it has to Create a sql file as below

delete from tabl1 where name = 'tom' and id = 1;
delete from tabl1 where name = 'tom' and id = 2;
delete from tabl1 where name = 'tom' and id = 3;
delete from tabl1 where name = 'tom' and id = 4;

delete.dat file will following data
1
2
3
4

when i passwd three argument for insert to same Shell script as

./shellscript.sh insert tom insert.dat

it has to Create a sql file as below

insert into tab1 values(id_sequence.nextval,'tom'1);
insert into tab1 values(id_sequence.nextval,'tom'2);
insert into tab1 values(id_sequence.nextval,'tom'3);
insert into tab1 values(id_sequence.nextval,'tom'3);

insert.dat file will following data
1
2
3
4

SQL file is based on arugument we pass(delete or insert)
.(After Creating the sql file it has to connect to database and execute the delete or insert sql script file.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 400 total points
ID: 35081332
Try the attached thingy.

wmp
#!/bin/bash

OP=$1
NAME=$2
INPUT=$3

OUTPUT=$OP.sql

if [ "$OP" = "delete" ]; then
   STRING='delete from tab1 where name = @$NAME@ and id = $VALUE\;'
 elif [ "$OP" = "insert" ]; then
   STRING='insert into tab1 values\(id_sequence.nextval,@$NAME@$VALUE\)\;'
  else
   echo "Wrong Operation!" ; exit
fi

if [ -z "$NAME" ]; then
   echo "Too Few Parameters Entered!"; exit
fi

if [ ! -e "$INPUT" ]; then
   echo "File $INPUT Not Found!"; exit
fi

while read VALUE
  do
   eval echo $STRING | tr "@" "'"
  done < $INPUT > $OUTPUT
exit

Open in new window

0
 
LVL 23

Assisted Solution

by:Maciej S
Maciej S earned 100 total points
ID: 35081377
A little bit shorter one:
#!/bin/sh

SQL_FILE=output.sql

if [ -z "${1}" -o -z "${2}" -o -z "${3}" ]; then
   printf -- "Syntax: %s delete|insert user file\n" "${0}"
   exit 1
fi

ACTION=${1}
USER=${2}
FILE=${3}

test -f ${FILE} && rm ${SQL_FILE}
cat ${FILE} | while read line; do
   case "${ACTION}" in
      delete) printf -- "%s from tabl1 where name = '%s' and id = %s;\n" "${ACTION}" "${USER}" "${line}" >> ${SQL_FILE};;
      insert) printf -- "%s into tab1 values (id_sequence.nextval,'%s'%s);\n" "${ACTION}" "${USER}" "${line}" >> ${SQL_FILE};;
   esac
done

Open in new window

0
 

Author Comment

by:vadicherla
ID: 35082115
As I mentioned it also connects to database and execute the SQL script and also spool the all database execution
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 400 total points
ID: 35082930
Add to the end of shellscript.sh

sqlplus dbuser/dbpass@SID <<EOF
set heading off
spool $OP.log
@./$OUTPUT
quit
EOF

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now