简体   繁体   English

bash shell脚本提示输入数据库密码

[英]bash shell script prompting for database password

I added a shell script I use all the time in cronjobs for a new account. 我添加了我在cronjobs中一直使用的shell脚本来创建新帐户。 When I ran it (under sudo -u) I got prompted for the database password. 当我运行它(在sudo -u下)时,提示我输入数据库密码。 When supplied with pass the command completed without issue. 当提供pass时,命令完成而没有问题。

The random password the host had assigned started with a $ and I wondered if that was issue so I modified to have pass in variable. 主机分配的随机密码以$开头,我想知道这是否有问题,所以我修改为传递变量。 The current script is below. 当前脚本如下。

When run it still prompts for db password before successfully completing. 运行时,它仍会提示您输入数据库密码,然后才能成功完成。 What is wrong there? 那里怎么了

#!/bin/sh

DBPASS="$randomlettersarehere"

/usr/bin/mysqldump --opt -h mysql.mysite.com -u myuser -p$DBPASS mydbname > "/home/mysite/backups/"`/bin/date +\%Y\%m\%d`.mysiteBackup.sql

Use single quotes to prevent $letters from being interpreted as a variable name. 使用单引号防止$letters解释为变量名。

DBPASS='$randomlettersarehere'

When you use double quotes the shell performs variable interpolation. 当使用双引号时,shell将执行变量插值。 With the dollar sign at the start, that means $DBPASS ends up being an empty string. 以美元符号开头,这意味着$DBPASS最终是一个空字符串。

以防它像我一样帮助别人:字符串以$开头,因此bash不会将sring插值为未定义变量

DBPASS='$randomlettersarehere'

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

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