ttyに「silent」を使用するのはいつ役に立ちますか?

ttyに「silent」を使用するのはいつ役に立ちますか?

このコマンドを実行すると、tty --help次のように表示されます。

tty --help
Usage: tty [OPTION]...
Print the file name of the terminal connected to standard input.

  -s, --silent, --quiet   print nothing, only return an exit status
      --help     display this help and exit
      --version  output version information and exit

したがってtty -s、実行された場合は何も返されません。

質問

  • いつ役に立ちますか?静かなのためttyの か。

ベストアンサー1

#!/bin/sh

# Script that is run regularly from cron,
# but also sometimes from the command line.

interactive=false
if tty -s; then
    echo 'Running with a TTY available, will output friendly messages'
    echo 'and may be interactive with the user.'
    interactive=true
fi

# etc.

つまり、現在のシェルセッションの標準入力ストリームにTTYが接続されているかどうかをテストする方法を提供します。これは、スクリプトが標準入力ストリームを読み取ってユーザーと対話できることを示します。[ -t 0 ]テストまたは同等の方法を使用してこれを実行することもできますtest -t 0本物fd 0(標準入力)がTTYの場合。

この-sオプションとそのバリエーションは非標準です(POSIX仕様tty)、そしてOpenBSD マニュアルttyテストも言及されています-t(ここでははい標準):

-s
端末名を書かないでください。このオプションを指定すると、終了ステータスのみが影響を受けます。 -sオプションは、「test -t 0」コマンドには使用されなくなりました。

おすすめ記事