
情況:需要登入多個遠端伺服器,其中一些伺服器具有 Fish shell
要求:預設 shell 是 bash。如果我登入伺服器並且存在 Fish,則切換到 Fish shell,否則保留在 bash 上。
嘗試過.bashrc:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Source global definitions
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
# Update Path
export PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH:$HOME/.bin
# Open fish shell if present, otherwise stick to bash
if hash fish 2>/dev/null; then
# echo "Fish detected, shifting shell"
fish "$@"; exit
fi
但是,scp 似乎不起作用。當我嘗試 scp 檔案時,詳細輸出顯示它卡在這裡。
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_ADDRESS = en_US.UTF-8
debug1: Sending env LC_IDENTIFICATION = en_US.UTF-8
debug1: Sending env LC_MEASUREMENT = en_US.UTF-8
debug1: Sending env LC_MONETARY = en_US.UTF-8
debug1: Sending env LC_NAME = en_US.UTF-8
debug1: Sending env LC_NUMERIC = en_US.UTF-8
debug1: Sending env LC_PAPER = en_US.UTF-8
debug1: Sending env LC_TELEPHONE = en_US.UTF-8
debug1: Sending env LC_TIME = en_US.UTF-8
debug1: Sending command: scp -v -f test_file
最初我認為該echo
命令導致它無法工作,但沒有它它也無法工作。
答案1
若要bashrc
在取得文件的 shell 會話不具有互動性時退出文件,您可以在文件的頂部(或方便的位置)執行以下操作:
case "$-" in
*i*) ;;
*) return ;;
esac
中的值$-
是一串字母,表示目前設定的 shell 選項。如果i
字串中存在該字符,則 shell 是互動的。
這可能是必要的,因為正如 terdon 在評論中指出的那樣,Bash 將由sshd
SSH 守護程序啟動的 shell 會話視為特殊情況。
細節:為什麼 bashrc 檢查目前 shell 是否是互動的?
在檔案的更下方,您可以檢查 shell 是否fish
可用並啟動:
if command -v fish 2>/dev/null; then
exec fish
fi
請注意,fish
在某些系統上這可能是“Go Fish”遊戲:-)
關於使用command -v
:為什麼不用「哪個」呢?那該用什麼呢?