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
  • Could you show how you give var its value? – Kusalananda Sep 2 at 9:53
  • I've edited my question. – User123 Sep 2 at 10:07
up vote 1 down vote accepted

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.

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
  • 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. – User123 Sep 2 at 9:43

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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