# unix script with SQL statement problem

**URL:** <https://community.unix.com/t/unix-script-with-sql-statement-problem/306800>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [March 20, 2012, 2:42am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800 "2012-03-20T02:42:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![happyv](https://community.unix.com/letter_avatar/happyv/32/6_449c533f586939b6e5f246632a46421f.png) [@happyv](https://community.unix.com/u/happyv)\
**Post date:** [March 20, 2012, 2:42am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/1 "2012-03-20T02:42:51Z")

</div>

Hello,

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

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

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [March 20, 2012, 2:48am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/2 "2012-03-20T02:48:42Z")

</div>

> [@happyv](#):
>
> for file in 'cat $filelist.txt'

Single-quotes is the problem here. It should be backticks, like this:

```plaintext
for file in `cat $filelist.txt`

```

This method is not so good owing to useless use of cat. You can try the below method:

```plaintext
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)

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

```

---

<div class="post-metadata">

**Author:** ![happyv](https://community.unix.com/letter_avatar/happyv/32/6_449c533f586939b6e5f246632a46421f.png) [@happyv](https://community.unix.com/u/happyv)\
**Post date:** [March 20, 2012, 2:58am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/3 "2012-03-20T02:58:22Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [March 20, 2012, 3:18am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/4 "2012-03-20T03:18:33Z")

</div>

Please post the script you used.

---

<div class="post-metadata">

**Author:** ![happyv](https://community.unix.com/letter_avatar/happyv/32/6_449c533f586939b6e5f246632a46421f.png) [@happyv](https://community.unix.com/u/happyv)\
**Post date:** [March 20, 2012, 3:18am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/5 "2012-03-20T03:18:44Z")

</div>

> [@balajesuri](#):
>
> Single-quotes is the problem here. It should be backticks, like this:
> 
> ```plaintext
> for file in `cat $filelist.txt`
> 
> ```
> 
> This method is not so good owing to useless use of cat. You can try the below method:
> 
> ```plaintext
> 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)
> 
> ```plaintext
> echo "select FILENAME from FILE_TABLE where filename like '${file}%';"
> 
> ```

I am tried to use your suggested while, is not work :eek:

---

<div class="post-metadata">

**Author:** ![cero](https://community.unix.com/letter_avatar/cero/32/6_449c533f586939b6e5f246632a46421f.png) [@cero](https://community.unix.com/u/cero)\
**Post date:** [March 20, 2012, 3:29am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/6 "2012-03-20T03:29:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [March 20, 2012, 3:32am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/7 "2012-03-20T03:32:18Z")

</div>

@happyv: We won't be able to help unless you provide more inputs.

---

<div class="post-metadata">

**Author:** ![happyv](https://community.unix.com/letter_avatar/happyv/32/6_449c533f586939b6e5f246632a46421f.png) [@happyv](https://community.unix.com/u/happyv)\
**Post date:** [March 20, 2012, 3:52am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/8 "2012-03-20T03:52:32Z")

</div>

I am just tried to use my original script and based on your comment,

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

the output will be:

FILENAME  
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
ORIGMODIFYDATE  
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
A1-67630403  
20120317-18:09:52

FILENAME  
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
ORIGMODIFYDATE  
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
A2-04985959  
20120315-20:09:52

Is it possible to make the output look better? e.g file and time on the same link?

---

<div class="post-metadata">

**Author:** ![cero](https://community.unix.com/letter_avatar/cero/32/6_449c533f586939b6e5f246632a46421f.png) [@cero](https://community.unix.com/u/cero)\
**Post date:** [March 20, 2012, 4:36am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/9 "2012-03-20T04:36:38Z")

</div>

Your select-statment only fetches the column FILENAME, not ORIGMODIFYDATE...  
To answer your question we need to know what database system, and maybe what tool (SQL-client) you use to retrive the data. Your script creates some statments but does not issue them against the db.

---

<div class="post-metadata">

**Author:** ![happyv](https://community.unix.com/letter_avatar/happyv/32/6_449c533f586939b6e5f246632a46421f.png) [@happyv](https://community.unix.com/u/happyv)\
**Post date:** [March 21, 2012, 12:16am UTC](https://community.unix.com/t/unix-script-with-sql-statement-problem/306800/10 "2012-03-21T00:16:01Z")

</div>

all, it work now.
