Solved

Shell Script

Posted on 2011-03-08
5
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This is about my first experience with programming Arduino.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

617 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