Solved

Shell Script

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

734 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