【问题标题】:read Statement not working in Linux script阅读语句在 Linux 脚本中不起作用
【发布时间】:2020-10-15 23:30:54
【问题描述】:

我在 linux 中有一个脚本。 while 循环中的读取语句不起作用。我做了很多尝试,但无济于事。这是我的代码:

while read instanceInFile
do
    echo -e "$instanceInFile"
    read -sp "Enter Password for the instance: " paswrd
    mysql -uauser -p$paswrd -e "create user '$user'@'$ip' identified by '$user';"
done<"allInstances.out"

allInstances.out

是一个包含要运行的环境脚本列表的文件。当我运行脚本时,它显示以下错误

ERROR 1045 (28000): Access denied for user 'power-user'@'some-ip' (using password: YES)

虽然它不从用户那里读取。

【问题讨论】:

标签: mysql linux bash


【解决方案1】:

两个read 命令都在使用同一个流。您可以让第一个 read 访问不同的文件描述符:

while read -r instanceInFile <&3; do
   .... while body unchanged
done 3< allInstances.out

原始答案可能不适用:

您问题中的代码格式已关闭。你真的有吗

...
done <myfile >myfile

??

bash 在调用命令之前 处理重定向。输出重定向&gt; 会将文件截断为零字节 while 读取循环开始之前。因此,while 循环没有行可供读取。

你需要做的

...; done <myfile >tempfile && mv tempfile myfile

或者,使用 miscutils 包

...; done <myfile | sponge myfile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多