2017-08-23 153 views
2

我有一個名爲test.sh我的服務器上的腳本預期(問題讀取):SSH遠程命令未正常工作

#!/bin/bash 
read -p "Select an option [1-4]: " option 
echo "You have selected $option" 

當我通過ssh手動運行它,我看到:

[email protected]:~$ ssh [email protected] 
[email protected]'s password: 
[...] 
[email protected]:~# bash test.sh 
Select an option [1-4]: 48 
You have selected 48 

當我運行它作爲SSH遠程命令,我看到:

[email protected]:~$ ssh [email protected] 'bash test.sh' 
[email protected]'s password: 
48 
You have selected 48 

我不滿意這個輸出,因爲它是小姐ing Select an option [1-4]:提示字符串和從我得到的原始腳本test.sh包含很多像這樣的交互式對話字符串,我需要它們。

我知道read打印它的提示stderr所以我試圖用如果省略標準錯誤以下的情況下,命令啓動腳本,但輸出保持還是一樣:

ssh [email protected] 'bash test.sh >&2' 
ssh [email protected] 'bash test.sh' >&2 
ssh [email protected] 'bash test.sh 2>&1' 
ssh [email protected] 'bash test.sh' 2>&1 

爲什麼發生這種情況和如何讓SSH遠程命令按預期工作?

UPD

我已經改變了test.sh這樣:

#!/bin/bash 
echo Connected 
read -p "Select an option [1-4]: " option 
echo "You have selected $option" 

但產出仍下落不明的提示字符串:

[email protected]:~$ ssh [email protected] 'bash test.sh' 
[email protected]'s password: 
Connected 
66 
You have selected 66 
+0

我已更新該問題。提示仍然丟失。 – user619271

+0

如果您只想打印該行,則使用'echo'打印行,然後使用'read'讀取值。它是一種解決方法。 –

回答