Unix script to run a query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix script to run a query
# 1  
Old 02-04-2009
Unix script to run a query

Hi ,


I need help in creating the shell script for querying using a loop.
I haev written the code as follows . But i am getting an error : Line [41] doesn't required '(' . i have chagned but am getting again

#!/bin/ksh

##checking the Command Line Parameter
if test $1 = ""
then
echo "Config File is not provided as Input ">>/data/coml/logs/cnt_log.txt
else
# Checking the config file for the Parameters
#checking Table name
tablename=$(grep "Table Name" $1 | cut -d: -f2)
fi
if test $tablename = ""
then
tablename_error="Table name is missing"
fi

#Reading and checking Columns
columns=$(grep "Columns" $1 | cut -d: -f2)
if test $columns = ""
then
columns_error="Columns are not available in config file"
fi

echo "Table Name :"$tablename
echo "columns :"$columns
if [ $tablename = "" ] -o [ $columns = "" ]
then
echo "Parameters in Config file are Missing(Table Name, Columns are Missing:" | tee -a /data/coml/logs/dp_log.txt
exit 1
fi
#Actual Code

no_of_cols=$(echo $columns|awk -F, '{print NF}')
echo "No.of Columns :" $no_of_cols
var_loop=0
db2 connect to MLCDMD user fdrcrd using fdrcrd123
while test $var_loop -le $no_of_cols
do
field=$(echo $columns | cut -d, -f$var_loop)
db2 "select $field,count(*) 'cnt' from fdrc.$tablename"> $field.txt
var_loop= ( $var_loop ) + 1
done
exit
# 2  
Old 02-04-2009
Hey, try updating the red line:

Code:
#!/bin/ksh

##checking the Command Line Parameter
if test $1 = ""
 then
        echo "Config File is not provided as Input ">>/data/coml/logs/cnt_log.txt
else
   # Checking the config file for the Parameters
        #checking Table name
        tablename=$(grep "Table Name" $1 | cut -d: -f2)
fi
        if test $tablename = ""
        then
         tablename_error="Table name is missing"
        fi

        #Reading and checking Columns
        columns=$(grep "Columns" $1 | cut -d: -f2)
        if test $columns = ""
        then
          columns_error="Columns are not available in config file"
        fi

        echo "Table Name :"$tablename
        echo "columns    :"$columns
        if  [ $tablename = "" ] -o  [ $columns = "" ]
        then
          echo "Parameters in Config file are Missing(Table Name, Columns are Missing:" | tee -a /data/coml/logs/dp_log.txt
          exit 1
fi
        #Actual Code

 no_of_cols=$(echo $columns|awk -F, '{print NF}')
echo "No.of Columns  :"  $no_of_cols
          var_loop=0
          db2 connect to MLCDMD user fdrcrd using fdrcrd123
          while test $var_loop -le $no_of_cols
 do
                field=$(echo $columns | cut -d, -f$var_loop)
                db2 "select $field,count(*) 'cnt' from fdrc.$tablename"> $field.txt
varloop=$(( varloop + 1 ))
           done
exit

# 3  
Old 02-04-2009
Quote:
Originally Posted by ramji_leo
Code:
        if  [ $tablename = "" ] -o  [ $columns = "" ]


Code:
if  [ $tablename = "" ] ||  [ $columns = "" ]

Quote:
Code:
                var_loop= ( $var_loop ) + 1


Code:
var_loop=$(( $var_loop + 1 ))

# 4  
Old 02-04-2009
Assign without space between '=' and '(' ... var_loop=( $var_loop ) + 1

or use expr to increment the value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run sql query in shell script and output data save as delimited text

I want to run sql query in shell script and output data save as delimited text (delimited text would be comma) Code: SPOOL_FILE=/pgedw/dan.txt SQL=/pgedw/dan.sql sqlplus -s username/password@myhost:port/servicename <<EOF set head on set COLSEP , set linesize 32767 SET TRIMSPOOL ON SET... (8 Replies)
Discussion started by: Jaganjag
8 Replies

2. Shell Programming and Scripting

Run sql query after ssh in UNIX

I am running this test.ksh on server1. It successfully logins to server2 but runs the queries of query.sql on server1. query.sql is present in both server1 and server2 Can anybody please help. I need to run queries on server2 itself.:confused: Below is the test script... (10 Replies)
Discussion started by: shruthimithra
10 Replies

3. UNIX for Dummies Questions & Answers

Script to run sql query.

Please read How To Ask Questions The Smart Way (1 Reply)
Discussion started by: balu_279013
1 Replies

4. Shell Programming and Scripting

Shell script to run sql query having a long listing of parameters

Hi, I have a query regarding execution of a sql query having long listing of parameters ..I need to execute this query inside a shell script. The scenario is like.... Suppose I have a file abc.txt that has the card numbers..it could be in thousands.. then I need to fire a query like ... (12 Replies)
Discussion started by: vsachan
12 Replies

5. Shell Programming and Scripting

Run SQL thru shell script: how to get a new line when run sql query?

Hi, this's Pom. I'm quite a new one for shell script but I have to do sql on shell script to query some information from database. I found a concern to get a new line...When I run my script, it retrieves all data as wondering but it's shown in one line :( What should I do? I'm not sure that... (2 Replies)
Discussion started by: Kapom
2 Replies

6. Shell Programming and Scripting

How to run a SQL select query in Oracle database through shell script?

I need to run a SQL select query in Oracle database and have to capture the list of retrieved records in shell script. Also i would like to modify the query for certain condition and need to fetch it again. How can i do this? Is there a way to have a persistent connection to oracle database... (9 Replies)
Discussion started by: vel4ever
9 Replies

7. Shell Programming and Scripting

Need help to run sql query from a script..which takes input from a file

I need to run sql script from shell script which takes the input from a file and contents of file will be like : 12345 34567 78657 and query will be like : select seq_nbr from bus_event where event_nbr='12345'; select seq_nbr from bus_event where event_nbr='34567'; select seq_nbr... (1 Reply)
Discussion started by: rkrish
1 Replies

8. Shell Programming and Scripting

run query in shell script after ssh

Hi, I need to run sql query in shell script after getting connected to ssh. For that I connected to ssh through shell script using RSA keys done]. Now when I am running sql query, it's not working... but several other commands like 'ls'. 'mkdir', etc are working properly. Here is my code: ... (2 Replies)
Discussion started by: shekhar2010us
2 Replies

9. Shell Programming and Scripting

run sql query via perl script

Hello, If I run this command on the server it works. # dbc "update config set radio_enabled = 0;" how can I execute the same command in perl. I have defined the dbc path. Can any one please correct the last line. #!/usr/bin/perl #database path $dbc='/opt/bin/psql -Userver... (0 Replies)
Discussion started by: sureshcisco
0 Replies

10. Shell Programming and Scripting

run oracle query remotly with shell script

hello how to run shell script to excute oracle queries on remote db ? i have tried as following sqlplus -s user/password@remote_server "select query;" but not working also this one sqlplus -s user/password@remote_server `select query;` not working :( i add this line to run another... (4 Replies)
Discussion started by: mogabr
4 Replies
Login or Register to Ask a Question