Skip to content

Commit 530e0b9

Browse files
committed
Enhance the db-login script to accept I/O redirection.
1 parent c1ac1cb commit 530e0b9

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

db-login

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ ${0##*/}
99
Uses the db-credentials script to start a command line mysql
1010
session with the correct host, database, user and password.
1111
Should be run from the project root folder. Passes all command
12-
line arguments (except for \`-h\`) to mysql.
12+
line arguments (except for \`--help\`) and I/O redirection to
13+
mysql. This script can be used as a complete replacement for
14+
the normal mysql command, without having to worry about
15+
connection credentials.
16+
17+
Called without arguments, the command will log you into the
18+
default database used by the CakePHP app. Using I/O redirection,
19+
.sql files can be imported into that database as well:
20+
\`bin/db-login < my-file.sql\`
1321
1422
Usage:
1523
bin/${0##*/}
@@ -19,7 +27,7 @@ EOT
1927

2028
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
2129
}
22-
if [ "$1" = '-h' ]; then
30+
if [ "$1" = '--help' ]; then
2331
usage
2432
fi
2533

@@ -42,9 +50,9 @@ CMD="mysql --host=${DB_HOST} ${PORT_CLAUSE} --database=${DB_DATABASE} --user=${D
4250

4351
# Handle all the different ways to provide (or not provide) a password.
4452
if [[ $DB_PASSWORD =~ " |'" ]]; then
45-
$CMD -p"${DB_PASSWORD}" "$@"
53+
$CMD -p"${DB_PASSWORD}" "$@" <&0
4654
elif [ -n "${DB_PASSWORD}" ]; then
47-
$CMD -p$DB_PASSWORD "$@"
55+
$CMD -p$DB_PASSWORD "$@" <&0
4856
else
49-
$CMD "$@"
57+
$CMD "$@" <&0
5058
fi

0 commit comments

Comments
 (0)