I'm new to writing shell scripts.
I am attempting to create a database using a shell script. Here's the script:
#!/bin/bash
#create a new db
a="mysql -uuser -ppassword -e'create database $1;'"
exec $a
The command exec mysql -uuser -ppassword -e'create database databaseName;'
works in a shell, but when I sh
the script, I get the mysql help open...
I think the problem is in the quotes, the simple quote prevent the variable expansion. You can simply do like this in your script:
#!/bin/bash
#create a new db
mysql -u user -p password -e "create database $1;"
Or you can try to place all your mysql commands in a file, let's say "dbname.sql". And do this:
#!/bin/bash
#create a new db
mysql -u user -p password "$1" < "$1.sql"
if you like use exec to run commands this can be a possible solution
#!/bin/bash
#create a new db
programm="mysql"
parameter[0]="-ppassword"
parameter[1]="-uuser"
parameter[2]="-ecreate database $1;"
exec "$programm" "${parameter[@]}"
exec parameter are
exec [-a NAME] [-cl] [COMMAND] [ARG...] [REDIRECTION...]
command is $programm
and the array parameter
is the argument list.
听起来您需要使用'cat'命令和管道,而不是使用'exec'。
cat /path/to/my/file | mysql -h localhost -u root -padmin
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.