unix script with SQL statement problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers unix script with SQL statement problem
# 1  
Old 03-20-2012
unix script with SQL statement problem

Hello,

I have a script to get the information from database, however, it's look like the loop is not work, can someone help? Smilie

echo Input file list to check:
read filelist
for file in 'cat $filelist.txt'
do
echo "select FILENAME from FILE_TABLE where filename like '${file}'%;" >> output_file.txt
done
echo Completed Check!!


The filelist is:
list1
A1-67630403
A2-04985959
B3-04857858
B6-058744748
C10-59584583
# 2  
Old 03-20-2012
Quote:
Originally Posted by happyv
for file in 'cat $filelist.txt'
Single-quotes is the problem here. It should be backticks, like this:
Code:
for file in `cat $filelist.txt`

This method is not so good owing to useless use of cat. You can try the below method:
Code:
while read file
do
...
done < ${filelist}.txt

Also, I'm no expert at SQL, but shouldn't the echo statement be like this? (% with-in the single quotes)
Code:
echo "select FILENAME from FILE_TABLE where filename like '${file}%';"

# 3  
Old 03-20-2012
sorry about the typing mistake, the % should be removed, like this

echo "select FILENAME from FILE_TABLE where filename like '${file}';"

Also, I used backticks in unix, still not work :-(
# 4  
Old 03-20-2012
Please post the script you used.
# 5  
Old 03-20-2012
Quote:
Originally Posted by balajesuri
Single-quotes is the problem here. It should be backticks, like this:
Code:
for file in `cat $filelist.txt`

This method is not so good owing to useless use of cat. You can try the below method:
Code:
while read file
do
...
done < ${filelist}.txt

Also, I'm no expert at SQL, but shouldn't the echo statement be like this? (% with-in the single quotes)
Code:
echo "select FILENAME from FILE_TABLE where filename like '${file}%';"

I am tried to use your suggested while, is not work Smilie
# 6  
Old 03-20-2012
What does not work? Do you see an errormessage or do you get wrong results? What shell, OS and database-system are you using?
About the SQL-syntax: its either filename = \'${file}\' looking for exact matches or filename like \'${file}%\' looking for values that start with those strings.
# 7  
Old 03-20-2012
@happyv: We won't be able to help unless you provide more inputs.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX Sqlplus - Capture the sql statement about to run and execution status

Greetings Experts, I am on AIX using ksh. Created a unix script which generates the CREATE OR REPLACE VIEW ... and GRANT .. statements, which are placed in a single .txt file. Now I need to execute the contents in the file (there are around 300 view creation and grant statements) in Oracle and... (4 Replies)
Discussion started by: chill3chee
4 Replies

2. Shell Programming and Scripting

How to create SQL statement out of data using shell script?

Table TAB1 contains following example data (its a tree sitting in table data format & its driven based CHILD & PARENT column pick the RULE condition to generate the below SQL: CHILD PARENT SS MID MNM VNM RULE FLG 1 ? S1 ? ? V1 rule004 I 2 1 S1 ? ? V1 0 Z 3 1 S1 ? ? V1 1 Z ... (6 Replies)
Discussion started by: gksenthilkumar
6 Replies

3. Shell Programming and Scripting

UNIX variable to SQL statement

The following is my script : #!/bin/bash echo "please give app_instance_id" read app_instance_id echo "id is $app_instance_id" export app_id=app_instance_id sqlplus -s nnviewer/lookup@//nasolora008.enterprisenet.org:1521/LOAD3 @test.sql<<EOF SPOOL /home/tibco/MCH/Data/qa/raak/name.xls... (4 Replies)
Discussion started by: raakeshr
4 Replies

4. UNIX for Dummies Questions & Answers

SQL statement is not work on unix script

Hi, I have the following basic script. However, the statement (line 5) is not work. The output data is not able to set my request format a30. Any advise? :mad: echo " Column filename format a30"|sqlplus4 echo Input file list to check: read filelist for file in `cat $filelist.txt` do... (1 Reply)
Discussion started by: happyv
1 Replies

5. Shell Programming and Scripting

Read SQL statement in Script

Hi Guys.. need some urgent help... I am stuck in something badly I need to write a script which would read a sql statement (which might be a join/inner join/select/sub select etc. ) I need to read that sql statement ... and in the output I want all the table names and columns (doesn't... (4 Replies)
Discussion started by: freakygs
4 Replies

6. Shell Programming and Scripting

Unix shell command output to a sql statement

Can i do this Say one command sed 's/:*/ /g' $summf is returning C1234 C2345 C3434 some no of rows, now this ouput i have to insert it into a DB table how do i do this?? (2 Replies)
Discussion started by: depakjan
2 Replies

7. Shell Programming and Scripting

How to call an sql script inside a while statement in KSH

Hi all, I'm trying to run an sql inside a loop which looks like this #!bin/ksh while IFS=, read var1 var2 do sqlplus -s ${USERNAME}/${PASSWORD}@${ORACLE_SID} << EOF insert into ${TABLE} ( appt_date ) values ( '${var1 }' ); ... (6 Replies)
Discussion started by: ryukishin_17
6 Replies

8. Shell Programming and Scripting

using SELECT sql statement in shell script

Hi there I have a database on a remote box and i have been using shell script to insert data into it for example, i could have a script that did this SN=123456 n=server1 m=x4140 sql="UPDATE main SET hostname='$n',model='$m' WHERE serial='$SN';" echo $sql |/usr/sfw/bin/mysql -h... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

9. Shell Programming and Scripting

Executing a Oracle SQL statement in a UNIX script

Hi All, I need to select one column from a table based upon the passed in parameter. I tried this: sqlplus -silent $MISP_USER << EOF set feedback off; set verify off; set sqlprompt "" SELECT mail_flag FROM dailyjobs WHERE job_name = '$1'; exit 0 EOF exit... (1 Reply)
Discussion started by: ganga.dharan
1 Replies

10. Shell Programming and Scripting

Creating an sql statement from a file. Problem with '

Hi, I am trying to create sql statements from a file, but I have a problem with ': This is what I do: cat filex.txt | awk -F: '{print $1,"A","and personnavn like",$5}' | sed -e "s/^/select bruker.brukernavn, person.personnavn from bruker, person where brukernavn like '/" -e "s/$/' and... (2 Replies)
Discussion started by: hannem
2 Replies
Login or Register to Ask a Question