0

I have typical issue with assign into variable select statement, I've tried many options, but none of them work..

I have function which replace column name on upper(column name).

Upper=(Column1 Column2)  # Array variable

upper_function()
{
query="$1"
for ((i=0;i<${#Upper[@]};i++))
do
query=$(echo "$query" | sed -e "s/\b"${Upper[$i]}"\b/UPPER(${NAME[$i]})/g")
done
SQLQUERY="$query"
}

Below I have SQL statement:

SQLQUERY="Select column1,column2 from table"

Now, I would like to change column1 and column2 to

upper(column1) and  upper(column2). 

So I run function:

upper_function "$SQLQUERY"

Solution above works fine, but the case is that instead of

SQLQUERY="$query"

I would like to make my function more automatic and assigning name of Select statement into variable eg like below:

$2="$query"

and then run function:

upper_function "$SQLQUERY" "SQLQUERY"

But it is not working and I have no idea why, how to assign that variable properly? Thanks

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
andreww
  • 223
  • 6
  • 16
  • `Upper=(Column1,Column2)` has only one element, `Column,Column2` -- if you wanted those to be two separate elements, you'd need a space between them: `Upper=( Column1 Column2 )` – Charles Duffy Jul 28 '16 at 12:31
  • Charles, I did that by accident (wrong copy and paste). I edited my question, array variable works fine now. Still the problem with variable assign – andreww Jul 28 '16 at 12:33
  • ...now, do you just want to be able to pass the *name* of a variable to use as both source and dest? That's easy enough. – Charles Duffy Jul 28 '16 at 12:35
  • Yes, that's what I want to do is run my function as upper_function "$SQLQUERY" "SQLQUERY" , so 2 variables... – andreww Jul 28 '16 at 12:39
  • Ye, sure you are right, I will not edit my question without explanation what was edited – andreww Jul 28 '16 at 12:39
  • *usually*, explanation about what was edited is a bad thing, where it interrupts the flow of reading (edit history is fully available for anyone curious); it's a question of whether it makes anything else no longer make sense. – Charles Duffy Jul 28 '16 at 12:41
  • You are totally right. Back to my main question, could you support me ? – andreww Jul 28 '16 at 12:42
  • been working on a tested answer while we were talking. Patience, patience. :) – Charles Duffy Jul 28 '16 at 12:43
  • ...the main holdup, as it turned out, was the `Column1` and `column1` names given in the array and the query not matching. – Charles Duffy Jul 28 '16 at 12:44
  • BTW, arguably there were two separate questions in here, one of them about how to perform indirect assignments (assigning to a named variable), and another one about using arrays. In an ideal world, you'd have asked them as separate questions, which anything unrelated to that specific item factored out (so actually no mentions of SQL at all) -- that way we could have pointed you at preexisting Q&A entries directly on-point for each of the problems at hand. – Charles Duffy Jul 28 '16 at 12:45

1 Answers1

1

This is, at its core, BashFAQ #6.

printf -v can be used for indirect assignments in modern (3.1+) versions of bash:

Upper=( column1 column2 )  # Array variable

upper_function() {
  local colname query=$1 varname=$2
  for colname in "${Upper[@]}"; do
    query=$(echo "$query" | sed -e "s/\b"${colname}"\b/UPPER($colname})/g")
  done
  printf -v "$varname" %s "$query"
}

...thereafter:

upper_function "$SQLQUERY" SQLQUERY

On older shells, instead:

eval "$varname=\$query"

...note that you need to trust the value of $varname to not be malicious, as code injected into that variable will be run by the function. Thus, you would never want to use a filename, table name, or other potentially-attacker-controlled variable to name a variable assigned to in this way.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Back to your last message : there was only one question about indirect assignments (first time hear about smth like that). First thing I did was searching for the information in google, but nothing found, nothing worked so i decided to put that question here. However thanks a lot for you help Regards – andreww Jul 28 '16 at 12:53
  • Now i notice that you edited you answer. thanks for the explanation. Regards – andreww Jul 28 '16 at 12:54