用于在 MySQL 中插入值的 Bash 脚本 [英] Bash script to insert values in MySQL

查看:21
本文介绍了用于在 MySQL 中插入值的 Bash 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个连接到我的 MySQL 服务器并从 txt 文件中插入一些值的 bash 脚本.我已经把它写下来了:

I want to make a bash script that connects to my MySQL server and inserts some valuse from a txt file. I have written this down:

#!/bin/bash
echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('cat test.txt');" | mysql -uroot -ptest test;

但我收到以下错误:

第 1 行的错误 1136 (21S01):列计数与值计数不匹配在第 1 行

ERROR 1136 (21S01) at line 1: Column count doesn't match value count at row 1

我想错误出在我的 txt 文件中,但我已经尝试了很多变体,但仍然没有成功的希望.

I suppose the error is in my txt file, but I've tried many variations and still no hope of success.

我的txt文件是这样的:

My txt file looks like this:

10.16.54.29 00:f8:e5:33:22:3f 玛莎拉

10.16.54.29 00:f8:e5:33:22:3f marsara

推荐答案

试试这个:

#!/bin/bash
inputfile="test.txt"
cat $inputfile | while read ip mac server; do
    echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('$ip', '$mac', '$server');"
done | mysql -uroot -ptest test;

通过这种方式,您可以流式传输文件读取以及 mysql 命令执行.

This way you streaming the file read as well the mysql comand execution.

这篇关于用于在 MySQL 中插入值的 Bash 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆