bash shell과 perl 관련 질문입니다 :)

-@Naver의 이미지

shell과 perl 곰부를 시작한지 1주 정도된 학생입니다.

타 서버에 등록된 정보를 기존 서버에 동기화하는 코드를 만들다 막히는 부분이 있어 질문 올려봅니다...

밑과 같이 'symout.txt'라는 이름의 파일에 정보(데이터)를 저장하여 기존 서버에 가져온 후,

!/usr/bin/perl
use DBI;
use Time::Local;
 
$database = "gg";
$user = "tj";
$password = "gg";
$option = "000.000.000.00";
 
$dsn = "DBI:mysql:$database";
$dsn = "DBI:mysql:database=$database;$option";
$dbh = DBI->connect($dsn, $user, $password);
 
open FP, ">", "symout.txt" or die "$!\n";
my $sql=$dbh->prepare("SELECT UserName, password, realname, expiration, station, subject, profid, ssid_group_name, e$
 
     my $rowcount = $sql->execute;
     while ( @row = $sql->fetchrow_array() ) {
            my $UserName = $row[0];
            my $password = $row[1];
            my $realname = $row[2];
            my $expiration = $row[3];
            my $station = $row[4];
            my $subject = $row[5];
            my $profid = $row[6];
            my $ssid_group_name = $row[7];
            my $enableuser= $row[8];
 
     print FP "$UserName:$password:$realname:$expiration:$station:$subject:$profid:$ssid_group_name:$enableuser\n";
     } close FP;
 
$sql->finish;
$dbh->disconnect();
exit 1;

결과값
------------ cat symout.txt -------------
user:123456789:test:0000-00-00 00:00:00:NO:NO:1:default:1

가져온 데이터를 기존 서버 'gg'라는 db에 레코드 값으로 데이터 값들이 각각 들어갈 수 있게
끔 넣으려고 하는데요.
(즉, 기존 서버 sql 에서 'select * from gg'를 입력하였을 때 가져온 데이터 값이 나오도록)

이 과정을 코드로 짜보았는데 아무리 검색을 해보아도 어떻게 해야 할 지 감이 잡히지 않아
도움을 받을 수 있을까 하여 질문 올려봅니다...

perl과 shell 파일로 각각 만들어 한번에 처리할 수 있도록 하는 방향으로 코드를 만들어 보았습니다...

------------ psym.pl -----------

#!/usr/bin/perl
use DBI;
use Time::Local;
 
$database = "gg";
$user = "root";
$password = "hh";
$option = "localhost";
 
$dsn = "DBI:mysql:$database";
$dsn = "DBI:mysql:database=$database;$option";
$dbh = DBI->connect($dsn, $user, $password);
 
# 파일 떨구기.
open FP, ">", "symout.txt" or die "$!\n";
 
 
#요 사이에 shell도 한번에 작동시킬 수 있는 코드 넣어야할 것 같다...
my $sql = $dbh->prepare("SELECT type, model, serial_no, order_user, order_date, send_user, send_date, last_client, i$
my $rowcount = $sql->execute;
while ( @row = $sql->fetchrow_array() )
        {
                my $type = $row[0];
                my $model = $row[1];
                my $serial_no = $row[2];
                my $order_user = $row[3];
                my $order_date = $row[4];
                my $send_user = $row[5];
                my $send_date = $row[6];
                my $last_client = $row[7];
                my $installation_locate = $row[8];
                my $info = $row[9];
 
                print FH "$type:$model:$serial_no:$order_user:$order_date:$send_user
                         :$send_date:$last_client:$installation_locate:$info\n";
        }
        close FH;
 
$sql->finish;  $dbh->disconnect();
exit 1;

------------ psym.sh -----------

#!/bin/bash
cat symout.txt | while read line
do
      echo $line
done

raymundo의 이미지

잘 이해가 안 되는데... SELECT 를 수행하셨던 것과 동일한 방법으로 INSERT 를 하시면 되겠네요.

INSERT 예제는 다음 페이지를 참고하시면 될 것 같고,
https://ko.perlmaven.com/simple-database-access-using-perl-dbi-and-sql

그러면 bash 셀스크립트는 쓸 일이 없어보이는데요?

좋은 하루 되세요!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.