将用户添加到Mariadb的脚本

发布于 2025-02-06 17:49:33 字数 519 浏览 0 评论 0原文

我正在尝试将用户添加到Mariadb中。男性用户使用航空公司作为密码,而女性则使用大陆作为密码,所有此信息均已通过CSV文件进行。

由于某种原因,我的脚本不起作用。这是脚本:

#!/bin/bash -p

FILENAME=mgarr048.csv  

while IFS=: read -r username first last gender dob state municipality season continent elective f1 airline
do
    (( gender == "m" )) || continue
    mysql -e GRANT INSERT ON *.* to $username@'%' IDENTIFIED by $airline;
done 

CSV标题

username,first,last,gender,dob,state,municipality,season,continent,elective,f1,airline

I am trying to add users into mariadb. Male users use an airline as password and females use a continent as a password all this info is taken form a csv file.

For some reason my script is not working. here is the script:

#!/bin/bash -p

FILENAME=mgarr048.csv  

while IFS=: read -r username first last gender dob state municipality season continent elective f1 airline
do
    (( gender == "m" )) || continue
    mysql -e GRANT INSERT ON *.* to $username@'%' IDENTIFIED by $airline;
done 

csv headers

username,first,last,gender,dob,state,municipality,season,continent,elective,f1,airline

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2025-02-13 17:49:33

在您的脚本中,您没有任何地方告诉它阅读文件名。这样修改您的脚本:

#!/bin/bash

FILENAME="mgarr048.csv"

while IFS=, read -r username first last gender dob state municipality season continent elective f1 airline
do
    if [[ "$gender" != "m" ]]
    then
        echo "mysql -e GRANT INSERT ON *.* to $username@'%' IDENTIFIED by "$airline""
    fi
done < "$FILENAME"

编辑

由于我没有您的数据库进行测试,因此我将echo 添加到mysql line。将其删除以在您的系统上使用。如果收到mySQL错误,它不是脚本,它与您的mySQL命令和/或数据库有关。

使用该数据集测试:

username1,user1,name1,m,1111-11-11,State1,City1,season1,continent1,elective1,f1,airline1
username2,user2,name2,f,2222-22-22,State2,City2,season2,continent2,elective2,f2,airline2

输出:

mysql -e GRANT INSERT ON *.* to username2@'%' IDENTIFIED by airline2

Nowhere in your script you tell it to read FILENAME. Modify your script like this:

#!/bin/bash

FILENAME="mgarr048.csv"

while IFS=, read -r username first last gender dob state municipality season continent elective f1 airline
do
    if [[ "$gender" != "m" ]]
    then
        echo "mysql -e GRANT INSERT ON *.* to $username@'%' IDENTIFIED by "$airline""
    fi
done < "$FILENAME"

EDIT

Since I do not have your database to test, I added echo to the mysql line. Remove it to use this on your system. If you get a mysql error, it is not the script, it is related to your mysql command and/or database.

Tested with that data set:

username1,user1,name1,m,1111-11-11,State1,City1,season1,continent1,elective1,f1,airline1
username2,user2,name2,f,2222-22-22,State2,City2,season2,continent2,elective2,f2,airline2

Output:

mysql -e GRANT INSERT ON *.* to username2@'%' IDENTIFIED by airline2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文