warning: Creating default object from empty value in /var/www/ on line 33.

mysql grant subqueries

MySQL doesn't allow subqueries inside GRANT statements (only inside SELECT, INSERT, UPDATE, DELETE, SET, or DO). Here's a workaround using bash, when you want to grant based on a wildcard pattern in the schema name:

mysql -e "grant select on $(mysql -N -e "select distinct table_schema from information_schema.tables where table_schema like 'foo%';").* to user@host;"

BASH trick: exit codes for piped commands via $PIPESTATUS


Here's an example pipe:

$ true | false | true | false

$? will return 1 because that is the exit code of the last command in the pipe (false)

If you need to access the exit code of previous commands in the pipe, use the BASH built-in array $PIPESTATUS. For example, to get the exit code of the first command ("true" in this example), use


And so on.

$ true | false | true | false
$ echo ${PIPESTATUS[*]}
0 1 0 1

To save $PIPESTATUS in another variable:

$ true | false | true | false
$ foo=( ${PIPESTATUS[*]} )
$ echo ${foo[*]}
0 1 0 1
Syndicate content