Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What i want to do is create multiple informix sql statements & later run them via a bash script.

Here is what i have so far (this works) but is meant to be run from a cron job so it only does one day at a time:

echo "
update hst`date --date='yesterday' +%m%d%y` 
   set x = 'GARP' 
 where y in ('CRE', 'LAC', 'SRL', 'JAG', 'JNM', 'BIM')
   and appl = '';
" > update_x_hst.sql
chmod 777 update_x_hst.sql
isql -s history_hst update_x_hst.sql
rm update_x_hst.sql

I have to do it this way because the system i am working with creates tables everyday, so i'll have hst070810, hst070910, hst071010, and so on as my tables that i need to update.

I want to do something that will create a sql file and then run it and then go do it again and again for a given number of times, in a for loop.

#!/bin/bash
for x in {1..11}
do
    echo "
    update hst`date --date='$x days ago' +%m%d%y` 
       set x = 'Complete' 
     where y = 1;
    " > update_x_hst.sql
    chmod 777 update_x_hst.sql
    isql -s history_hst update_x_hst.sql
    rm update_x_hst.sql
done

The problem here is it doesnt want to read the variable $x correctly so the statement fails.

Any ideas?

share|improve this question
2  
Don't chmod 777. It's a security risk. –  Daenyth Jul 28 '10 at 22:53

1 Answer 1

Variables aren't expanded inside single quotes. Change them to double quotes and it'll work.

#!/bin/bash
for x in {1..11}
do
    echo "
    update hst$(date --date="$x days ago" +%m%d%y)
       set x = 'Complete' 
     where y = 1;
    " > update_x_hst.sql
    chmod 777 update_x_hst.sql
    isql -s history_hst update_x_hst.sql
    rm update_x_hst.sql
done

Also, you should set the permission to something more appropriate. It's not likely that 777 is necessary.

I'm not sure, but you can probably pipe that SQL into isql and avoid creating, chmoding and rming a temporary file.

echo "something" | isql ...
share|improve this answer
    
Thanks that works! You're right about the permissions and piping, i just wanted to work on the hardest part first before going into the nitty gritty. –  rumz Jul 28 '10 at 23:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.