1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

標準出力を受け取りコマンドを実行する Go

Posted at

#[|]で値を流してreadで変数に格納し、変数の値のファイルを3つ作成する
Linuxコマンド

./request | ./scan | sh mkfile.sh

キャプチャ7.PNG
request.go  ランダムにUUIDを生成して標準出力する
scan.go   UUIDから数値のみを抽出して標準出力
mkfile.sh   標準出力から受け取った数値のfileを3つ作成する

今回初めてスクリプトで標準出力を受け取りLinuxコマンドを実行したので覚書です。

request.go

package main

import(
        "fmt"
        "time"
        crand "crypto/rand"
        "math"
        "math/rand"
        "math/big"
)
func main() {
        for {
            seed, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
            rand.Seed(seed.Int64())
            s :=rand.Int63()
            fmt.Printf("UUID=%v\n",s)
            time.Sleep(1 * time.Second)
        }
}

scan.go

package main

import (
    "fmt"
    "strings"
)
func main() {
    var a string
    dst := ""
    for {
    fmt.Scan(&a)
    if strings.Contains(a,"UUID"){
        dst = strings.Replace(a, "UUID=", "", 1)
        fmt.Println(dst)
    } else {
        fmt.Printf("none\n")
    }
    }
}

mkfile.sh

#!/bin/bash

echo "Enter files name"
read file_name1
echo "get file_name1"
read file_name2
echo "get file_name2"
read file_name3
echo "get file_name3"


touch $file_name1
touch $file_name2
touch $file_name3

凄く基本的な部分ですが、なんとなくスクリプトのイメージができるようになりました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?