Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell Script

Posted on 2011-03-08
5
Medium Priority
?
375 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 1600 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 400 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 1600 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project

885 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