リモート SSH コマンド - bash バインド警告: 行編集が有効になっていません

リモート SSH コマンド - bash バインド警告: 行編集が有効になっていません

私はbash 4.3.11(1)を使用しており、次の履歴プラグインをインストールしています(.bash_it):

# enter a few characters and press UpArrow/DownArrow
# to search backwards/forwards through the history
bind '"^[[A":history-search-backward'
bind '"^[[B":history-search-forward'

対話型セッションにログインするとすべて正常ですが、ssh host 'ls -als'たとえばを介してリモート コマンドを実行すると、次の出力が表示されます。

: ssh host 'ls -als'
/home/ubuntu/.bash_it/plugins/enabled/history.plugin.bash: line 3: bind: warning: line editing not enabled
/home/ubuntu/.bash_it/plugins/enabled/history.plugin.bash: line 4: bind: warning: line editing not enabled

各バインド呼び出しの後に履歴プラグインを変更すると、echo -e '\0033\0143'警告は表示されなくなりますが、コンソールはクリアされます。大きな欠点ではありませんが、リモート コマンドに対してこれを抑制するよりクリーンな方法を知っておくと便利です。

# Works, but annoyingly clears console
# enter a few characters and press UpArrow/DownArrow
# to search backwards/forwards through the history
bind '"^[[A":history-search-backward'
echo -e '\0033\0143'
bind '"^[[B":history-search-forward'
echo -e '\0033\0143'

答え1

対話型セッションだけではbind動作しません。たとえば、emacs シェルはif [ -t 1 ]テストに合格する対話型セッションを提供しますが、行編集機能がないため、bind内の は~/.bashrc警告を生成します。代わりに、次のようにして行編集が有効になっているかどうかを確認できます (より簡単な/良い方法はありますか?):

if [[ "$(set -o | grep 'emacs\|\bvi\b' | cut -f2 | tr '\n' ':')" != 'off:off:' ]]; then
  echo "line editing is on"
fi

答え2

ssh host 'ls -als'

ssh にリモート システムでコマンドを実行するように指示すると、ssh は通常、リモート セッションに PTY (疑似 TTY) を割り当てません。 ssh を次のように実行して、-t強制的に tty を割り当てることができます。

ssh -t host 'ls -als'

常にこれを入力したくない場合は、ローカル ホストの「.ssh/config」ファイルに次の行を追加できます。

RequestTTY yes

あるいは、リモート システムの「.bashrc」ファイルを修正して、セッションが対話型でない場合に対話型であると想定するコマンドが実行されないようにすることもできます。 1 つの方法は、セッションに TTY があるというテストでコマンドを囲むことです。

if [ -t 1 ]
then
    # standard output is a tty
    # do interactive initialization
fi

答え3

行編集がない場合、これらのbindコマンド自体は無害です。警告を抑制します。

bind '"^[[A":history-search-backward' 2>/dev/null
bind '"^[[B":history-search-forward'  2>/dev/null

これはいくぶん洗練されていないが、それでも機能するはずだ。他の回答は、最良/十分なテストについては同意していない。私のアプローチはこれを回避します。ただし、拡張性は高くありません。2 つのコマンドだけでは大きな違いは生じないはずですが、数十個など、もっと多い場合は、適切な条件文の方がおそらく良いでしょう。

答え4

おそらく、bash がインタラクティブかどうかをテストするだけでしょう。

if [[ "$-" = *i* ]]
then
    debug "interactive mode"
    source ~/.bash_lib/bindings
else
    debug "non interactive mode"
fi

関連情報