-1

我写了一个脚本,但我不断收到以下错误。尝试连接时用户'zul'@'localhost(使用密码:是)的访问被拒绝

项目:43:[d:未找到

项目:43:[d:未找到

它没有读取输入 d 也没有连接到 MySQL。有任何想法吗?

这是我的脚本:

#!/bin/bash

tstamp=$(date +$s) 

#Dump or Update
if [ -z "$1" ]; then
  echo -n "What do you want to do? [D]ump or [U]pdate: "
  read parm
else
  parm=$1
fi

#Database Name
if [ -z "$2" ]; then
  echo -n "What database are you using?"
  read dbname
else
  dbname=$2
fi

#Database User
if [ -z "$3" ]; then
  echo -n "What database user are you using?"
  read dbuser
else
  dbuser=$3
fi

#Database Password
if [ -z "$4" ]; then
     echo -n "What password are you using?"
     read dbpass
else
     dbpass=$4
fi


#Checks if the directory exists
if test ! -d "/home/zul/sql/backup"; then
  mkdir /home/zul/sql
  mkdir /home/zul/sql/backup
fi

mysqldump $dbuser -p$dbpass $dbname > /home/zul/sql/backup/$tstamp.$dbname.sql

if [$parm = d ]; then
  mysqldump $dbuser -p$dbpass $dbname > /home/zul/sql/backup/$dbname.sql
elif [$parm = u ]; then
  mysqldump $dbuser -p$dbpass $dbname < /home/zul/sql/backup/$dbname.sql
else
  echo " d or u option only! (lower case)"
fi
4

2 回答 2

2

我猜你的 if-else 测试中缺少一个空格:
应该是[ test ]而不是[test ]

if [ $parm = d ]; then
  mysqldump $dbuser -p$dbpass $dbname > /home/zul/sql/backup/$dbname.sql
elif [ $parm = u ]; then
  mysqldump $dbuser -p$dbpass $dbname < /home/zul/sql/backup/$dbname.sql
else
于 2012-07-28T16:25:38.637 回答
1

您必须使用空格将 '[' 与其参数分开:

if [ $parm = d ]; then
    mysqldump $dbuser -p$dbpass $dbname > /home/zul/sql/backup/$dbname.sql
elif [ $parm = u ]; then
于 2012-07-28T16:27:04.467 回答