Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, all. I am trying to write a ksh script that accepts an SQL query from the user like this: Code: script_name select * from schema.table; When the script runs, ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    11

    turn off globbing within script

    Hi, all.

    I am trying to write a ksh script that accepts an SQL query from the user like this:

    Code:
    script_name select * from schema.table;
    When the script runs, the user will be prompted for a password to the database and then the query will run and return results. The problem is, the wildcard is returning a list of all the filenames in my current directory. Therefore, I need to turn off globbing.

    I have found advice on the internet but none that apply to me. First, I need to turn off globbing within my script so I cannot use "set -f". Also, I cannot expect the user to input the * as '*'. Is there a way to accept a query normally as I have above? Here's my code:


    Code:
    if [ -n "$*" ]; then
      QUERY="$*"
    fi
    
    mysql -usystem -p$password -e "$QUERY"
    I have tried the following and none of them work:


    '$QUERY' => $QUERY
    '"$QUERY"' => "$QUERY"
    "$QUERY" => glob
    "'$QUERY'" => glob
    $QUERY => glob

    Thanks for your help.

  2. #2
    Just Joined!
    Join Date
    Oct 2008
    Posts
    11
    I made a mistake, actually. I meant to say that I cannot user "set -o noglob" since I'm doing this in ksh.

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    The problem is the shell environment is expanding the * before running the script. So there's nothing you can do in the script to get that *. The only solution I see is have the script prompt the user for the SQL query instead having the user pass it as a paramter list.

  4. #4
    Just Joined!
    Join Date
    Oct 2008
    Posts
    11
    Thanks for your help. I might go with that or with forcing the user to put the query in single quotes after all.


    Code:
    if [ -n "$*" ]; then
      QUERY="$*;"
    fi
    
    mysql -u user_name -p$password -e "$QUERY"


    Code:
    script_name 'select * from schema.table'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...