1

I want to send a number of predefined queries to mysql. i will use the variable defined internally by using the externally inputted variable as below. But not good.

[testuser@testserver work]$ cat test1.sh 
#!/bin/sh

query1='select * from mysql.user limit 2;'
query2="select * from mysql.user limit 2;"

echo $1
echo "$1"
#mysql -uuser-p -e "$1"

this is result

[testuser@testserver work]$ sh test3.sh query1 
query1
query1

but i want result

[testuser@testserver work]$ sh test1.sh  query1 
select * From mysql.user limit 1

how to modify this bash scrpit?

3
  • 1
    U could use echo "${!1}" syntax! Commented Nov 4, 2019 at 8:50
  • Question: what is your ultimate goal here? Indirection is generally not the way to go. If you just want to define the variables query1 and query2, you can use source test1.sh; echo query1, if you just want to retrieve it, I would use a case statement. Commented Nov 4, 2019 at 10:31
  • @user1819769 : $1 is the first parameter to your script. You pass query1 as parameter, so of course query1 is printed by the echo statement. Commented Nov 4, 2019 at 10:46

3 Answers 3

4

Try simply:

#!/bin/bash

query1='select * from mysql.user limit 2;'
query2="select * from mysql.user limit 2;"

echo "${!1}"
#mysql -uuser-p -e "${!1}"

But you could:

#!/bin/bash

queries=('select * from mysql.user limit 2;'
        "select * from mysql.user limit 2;")

echo "${queries[$1]}"
#mysql -uuser-p -e "${queries[$1]}"

Then run ./test 0 or ./test 1.

Or even

#!/bin/bash

declare -A queries=$'(
    [query1]=\047select * from mysql.user limit 2;\047
    [query2]="select * from mysql.user limit 2;")'

echo "${queries[$1]}"
#mysql -uuser-p -e "${queries[$1]}"

Note: Syntax $'....\047...\047...' is a trick for using simple quotes in quoted string.

Then run ./test3 query1.

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

3 Comments

So $1 needs now to be numeric because we use an array?
@stephanmg In my second purpose, yes!
@F.Hauri: I'd wanted to suggest this too, because if he will have more queries this will somehow clutter the code.
1

Can also use a case statment:

#!/usr/bin/env bash


case "$1" in
    --query1)
        echo "select * from mysql.user limit 1;"
        ;;
    --query2)
        echo "select * from mysql.user limit 2;"
        ;;
    *)
        echo "unkown option"
        exit 1
        ;;
esac

then run something like: ./test --query1

1 Comment

Double dash are not needed! case $1 in query1 ) ...;; query2) ...;;esac will work too!
0

You can add the following lines:

#!/bin/bash

query1='query 1'
query2='query 2'

eval var="$"$1
echo $var

Edit: This uses eval which should be avoided, which might be unsafe if you don't trust the data. There is for Bash scripts variable indirection via the mechanism "${!VARNAME}", so you can use "${!1}".

2 Comments

Avoid using eval!!
Possibly interesting if not writing Bash but Sh scripts: unix.stackexchange.com/questions/425024/…

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.