Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am working on a script which dynamically executes some queries on daily basis. These queries are coming from a table in the database.

Here is the sample output of the query table:

+---------------+-------------------------------+---------+
| query_name    | query                         | userid  |
+---------------+-------------------------------+---------+
| All_User      | select * from users LIMIT 10; | jmatthe |
+---------------+-------------------------------+---------+

Now, I have to execute the query select * from users LIMIT 10; dynamically. I am reading each line of the output and storing the query from the output.

query_name=$(echo $query | cut -d\| -f1)
query_sql=$(echo $query | cut -d\| -f2)
query_user=$(echo $query | cut -d\| -f3)

Now here is where the problem arises. Because my line contains a * character in it, echo $query expands the * to replace it with the files in the current directory. So basically, my query_sql stores something like this.

select batchemail.sh query_output.txt from tbl_query

I want to preserve the * in the line so that I get the same in my query_sql variable. I want my query_sql variable to store the original data.

select * from tbl_query

Can anybody guide me on this?

share|improve this question
    
Feel free to edit your question instead of other's answers. –  petrus Apr 16 '13 at 11:30
add comment

2 Answers

up vote 10 down vote accepted

You disable it by adding the following line in your script:

set -o noglob

As an example,

echo *
your files and folders are shown here..
set -o noglob
echo *
*
share|improve this answer
    
Too bad. I thought escaping the * would help me, but now the query doesn't work. –  divinedragon Apr 16 '13 at 11:25
    
@divinedragon I don't understand your comment. What are you talking about? Does "now the query doesn't work" refer to the set -o noglob proposal or to something completely different which you did but didn't mind telling us...? –  Hauke Laging Apr 16 '13 at 11:28
    
I thought making the query as select \* would help me and the * would not expand. But later on, the select \* query didnt run on MySQL. With your option, I was able to do that. –  divinedragon Apr 16 '13 at 12:50
add comment

You can put it into "":

$ ls
file1  file2  file3  file4  file5  file6  file7  file8  file9
$ Q='select * from table;'
$ echo $Q
select file1 file2 file3 file4 file5 file6 file7 file8 file9 from table;
$ echo "$Q"
select * from table;
share|improve this answer
add comment

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.