0

I have a config.txt file with the following:

VAR=('a' 'b' 'c')
database='mydb'

and I want to use the VAR variable in the script (myscript.sh) to check if any of the data in it exists in a search that will be made on the database.

The search I am doing is like this:

output=$(mysql $database -u $user -p$pass -se "select name from my_table where word = '$VAR'")
2
  • You define VAR as an array but use it as a "scalar". What actual value are you wanting to use in that select statement? Do you want to iterate over the array? Commented Jul 20, 2017 at 12:47
  • I want to search the database for each one of the data stored in the variable VAR. One of them should return a result. Commented Jul 20, 2017 at 13:25

2 Answers 2

1

You can read files into your script with the source command (aliased simply as .) like so:

. config.txt
# or
source config.txt

To get a comma separated string, check for example here

Sign up to request clarification or add additional context in comments.

2 Comments

That won't quite work; it doesn't appear from its use that VAR is supposed to be a shell array.
VAR is supposed to have more than one data, therefore an array, and i want to use it to search the database for each one of them. How can i do that?
0

To expand on @Olli's answer:

source config.txt

# SQL injection
# values=$(printf "'%s'\n" "${VAR[@]}" | paste -sd,)
values=$(printf "'%s'\n" "${VAR[@]//\'/\'\'}" | paste -sd,)

select="select name from my_table where word in ($values)"
output=$(mysql "$database" -u "$user" -p"$pass" -se "$select")

4 Comments

each time i use source in the script i get: line 19: mysql: command not found line 21: ls: command not found
that sounds like you need to ask a separate question, and you have to show the code.
I created a script to test your solution, the only thing i have more is at the beginning the #!/bin/sh, the rest is the same as you wrote above. i got GNU is because of that?
Because of what? I don't understand your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.