1

So, i'm storing the values of the first column returned from a sql query in a variable(var)..it's supposed to be an array type, because the sql query returns multiple rows, and later,these values would be used in a command.

I'm not sure how to run the same command one after the other with different variables in a shell script.

var=`app/oracle/product/10.2.0/bin/sqlplus -s user/passwd<< !EOF!
  set pages 0 echo off feed off timing off
select entity_name,uid,set_date from table_name;
exit
     !EOF!`

The command to be run looks like:

X=$var Y=`date` ./test.sh
2
  • Could you show how you give var its value?
    – Kusalananda
    Commented Sep 2, 2018 at 9:53
  • I've edited my question.
    – ashish_k
    Commented Sep 2, 2018 at 10:07

2 Answers 2

2

Your request is unclear. Are you in a position to modify the script? If not, and if it explicitly depends on the X and Y variables, you'll need a temp / loop variable. To run test.sh for every array element, try a for loop (unless there are white space chars in any of the elements):

for TMP in "${var[@]}"; do X="$TMP" Y=$(date) ./test.sh; done
1
  • Thanks for the response..X and Y are the variables that i'm passing to the script: test.sh, so basically, the command to be run from the command prompt looks like: X=$var Y=$(date) ./test.sh. I'm not sure if in the loop, it'll check the successful completion of it's previous run.
    – ashish_k
    Commented Sep 2, 2018 at 9:43
1

You don't need to store the output of your database query in one variable.

app/oracle/product/10.2.0/bin/sqlplus -s user/passwd <<END_SQL | while IFS= read -r thing; do X="$thing" Y="$(date)" ./test.sh; done
set pages 0 echo off feed off timing off
select entity_name,uid,set_date from table_name;
exit
END_SQL

This would read the output of the sqlplus command and call your script once for each line, setting the value of the environment variables X and Y in the process.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .