1
0

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 1 year has passed since last update.

【Linux】[y/n]に自動で回答する方法

Last updated at Posted at 2022-10-29

はじめに

コマンドを実行した際に途中で[y/n]などが出力され回答を求められる場合があります。
これに自動的に回答する方法について記載します。

自動で回答する方法

yesコマンドを使用することで自動的に回答できます。

試しにyesコマンド単体で実行します。

$ yes
y
y
y
y
^C

中断するまでyが出力され続けます。

これを標準入力として次のコマンドに渡します。

$ yes | rm -i *.txt
remove example.txt? remove sample.txt? remove test.txt?

自動的にyと回答されてすべて削除されます。

y以外の回答をするには以下のようにyesコマンドの後に記述します。

$ yes 'n' | rm -i *.txt
remove example.txt? remove sample.txt? remove test.txt?

自動的にnと回答されます。

yes 'n'のみで実行すると以下のように中断するまでnが出力され続けます。

$ yes 'n'
n
n
n
n
n
^C

n以外の文字列も同様に出力されます。

$ yes 'foo'
foo
foo
foo
foo
foo
^C

なお一度だけ自動で回答できれば良い場合には以下のようにもできます。

$ echo 'y' | rm -i sample.txt
remove sample.txt?
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?