简体   繁体   English

具有多个变量的 Bash 脚本

[英]Bash Script with multiple variables

Here's what I'm trying to do:这是我想要做的:

  • Ask for username, database, and password for SQL为 SQL 请求用户名、数据库和密码
  • Attempt SQLDUMP尝试 SQLDUMP
  • If failed due to specific table, ask for which table, once entered, add it to script.如果由于特定表而失败,请询问哪个表,一旦输入,将其添加到脚本中。
  • Run again automatically after adding table添加表后再次自动运行
  • If failed again, ask for new table, add table to list如果再次失败,请求新表,将表添加到列表
  • Continue this process until successful继续这个过程直到成功

Here's what I've made so far:这是我到目前为止所做的:

#!/bin/bash

:dbase
echo "What is the Database name?"
read db1
goto :PW

:PW
echo "what is the password?"

read p1


PASSWORD=${p1}
HOST=mysql
USER=root
DATABASE=${db1}
DB_FILE=SQLDump.sql
EXCLUDED_TABLES=(
)

IGNORED_TABLES_STRING=''
for TABLE in "${EXCLUDED_TABLES[@]}"
do :
   IGNORED_TABLES_STRING+=" --ignore-table=${DATABASE}.${TABLE}"
done

echo "Dump structure"
mysqldump --compress --host=${HOST} --user=${USER} --password=${PASSWORD} --single-transaction --no-data --routines --extended-insert=false --max_allowed_packet=2048M ${DATABASE} > ${DB_FILE}

echo "Dump content"
mysqldump --compress --host=${HOST} --user=${USER} --password=${PASSWORD} --extended-insert=false --max_allowed_packet=2048M ${DATABASE} --no-create-info --skip-triggers ${IGNORED_TABLES_STRING} >> ${DB_FILE}
pause
alias ll='ls -lah'
ll *.sql

:choice
set /P c=Do you see your file[Y/N]?
if /I "%c%" EQU "Y" goto :yes
if /I "%c%" EQU "N" goto :no
goto :choice


:yes

echo "Awesome, I'll delete this script now."
rm -rf /srv/SQLDump
echo "I have successfully deleted SQLDUMP."
pause
exit

:no
echo "What was the name of the table that failed?"
read $table1
set /P c=Was there another?[Y/N]?
if /I "%c%" EQU "Y" goto :t2
if /I "%c%" EQU "N" goto :continue


PASSWORD=${p1}
HOST=mysql
USER=root
DATABASE=${db1}
DB_FILE=SQLDump.sql
EXCLUDED_TABLES=(
${table1}
)

IGNORED_TABLES_STRING=''
for TABLE in "${EXCLUDED_TABLES[@]}"
do :
   IGNORED_TABLES_STRING+=" --ignore-table=${DATABASE}.${TABLE}"
done

echo "Dump structure"
mysqldump --compress --host=${HOST} --user=${USER} --password=${PASSWORD} --single-transaction --no-data --routines --extended-insert=false --max_allowed_packet=2048M ${DATABASE} > ${DB_FILE}

echo "Dump content"
mysqldump --compress --host=${HOST} --user=${USER} --password=${PASSWORD} --extended-insert=false --max_allowed_packet=2048M ${DATABASE} --no-create-info --skip-triggers ${IGNORED_TABLES_STRING} >> ${DB_FILE}
pause
alias ll='ls -lah'
ll *.sql

:choice
set /P c=Do you see your file[Y/N]?
if /I "%c%" EQU "Y" goto :yes
if /I "%c%" EQU "N" goto :no
goto :choice

It's obviously not finished.显然还没有结束。 I ran into an issue where i'm calling the script over and over again in order to add multiple tables.我遇到了一个问题,我一遍又一遍地调用脚本以添加多个表。 I know there's a workaround, but as I'm self taught, I haven't been able to figure it out.我知道有一种解决方法,但由于我是自学的,我一直无法弄清楚。 Ideas?想法?

To restart your script it have to support options, so i'd suggest this approach要重新启动您的脚本,它必须支持选项,所以我建议使用这种方法

#!/bin/bash

# get options if restarter
while [[ $@ ]]; do
    case $1 in
        --name) name="$2";;
        --pass) pass="$2";;
        --tabl) tables+=("$2");;
    esac
    shift
done

# get initial data when started first time
[[ $pass ]] || read -p 'What is the password? '      pass
[[ $name ]] || read -p 'What is the Database name? ' name

options=( --name "$name" --pass "$pass" )

# your code here
# add tables to tables to options array like this
# options+=(--tabl "table name")

# and restart script with options like this

$0 "${options[@]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM