Solved

Shell Script

Posted on 2011-03-08
5
367 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

803 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