LoginSignup
0
0

More than 1 year has passed since last update.

2022-07-26 キーボードからコマンドに標準入力を受け渡す

Last updated at Posted at 2022-08-05

環境

$ bash --version
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

発生した問題

間違えて sort コマンドを単体で入力したときなど、コマンドが標準入力を受け付けた状態となる。

ctrl+c で抜ければいいのだが、何も出力なく中断されてしまう。なんか面白味がない。

$ sort
h
a
t
s
u
n
e

m
i
k
u

^C
$

キーボード入力を標準入力から受け渡して、それを sort させたい。

方法

ctrl+c ではなく ctrl+d を入力することで、キーボードで入力した内容を sort コマンドに受け渡すことができる。

ctrl+d を入力した後、 sort コマンドの結果が出力される。

$ sort
h
a
t
s
u
n
e

m
i
k
u

^D

a
e
h
i
k
m
n
s
t
u
u

余談

php 単体で実行した際の、標準入力を受け付ける処理においても利用できる。

$ php
<?php
class Hatsune
{
    public function run() 
    {   
        echo 'Hatsune Miku';
    }
}

$h = new Hatsune();
$h->run();
^D
Hatsune Miku

余談2

Windows の場合は ctrl+Z で同様の挙動となる。

C:\Users\taro_hida>php
<?php
class Hatsune
{
    public function run()
    {
        echo 'Hatsune Miku';
    }
}

$h = new Hatsune();
$h->run();
^Z
Hatsune Miku
0
0
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
0
0