我正在寫一個Bourne shell腳本,並有這樣的密碼輸入:如何關閉終端中的回聲?
echo -n 'Password: '
read password
顯然,我不希望被密碼回顯到終端,所以我想關閉回聲時間的閱讀。我知道有一種方法可以與stty
這樣做,但我會在閱讀聯機幫助頁時詢問社區的好處。 ;)
我正在寫一個Bourne shell腳本,並有這樣的密碼輸入:如何關閉終端中的回聲?
echo -n 'Password: '
read password
顯然,我不希望被密碼回顯到終端,所以我想關閉回聲時間的閱讀。我知道有一種方法可以與stty
這樣做,但我會在閱讀聯機幫助頁時詢問社區的好處。 ;)
stty_orig=`stty -g`
stty -echo
echo 'hidden section'
stty $stty_orig
這是我以前做過的方式,如果內存服務。出於好奇,這與「stty -echo」有什麼不同?讀foo; stty echo'? – 2011-04-12 12:14:37
是的,它保留了回聲後關閉的情況下,它已被關閉。 – ak2 2011-04-12 15:18:30
@ ak2:謝謝,非常有意義。 – 2011-04-14 05:39:11
read -s password
適用於我的linux系統。
這是有用的信息,但-s似乎是一個Bash擴展,因爲'read -s foo'產生'閱讀:1:Dash中的非法選項-s(Ubuntu符號鏈接到'/ bin/sh'的Bourne shell)。 – 2011-04-12 13:45:26
Bourne shell腳本:獲取更多信息
#!/bin/sh
# Prompt user for Password
echo -n 'Password: '
# Do not show what is being typed in console by user
stty -echo
# Get input from user and assign input to variable password
read password
# Show what is being typed in console
stty echo
stty的手動命令:
@:/dir #man stty
的stty手冊片段:
STTY(1) stty 5.2.1 (March 2004) STTY(1)
NAME
stty - change and print terminal line settings
SYNOPSIS
stty [-F DEVICE] [--file=DEVICE] [SETTING]...
stty [-F DEVICE] [--file=DEVICE] [-a|--all]
stty [-F DEVICE] [--file=DEVICE] [-g|--save]
DESCRIPTION
Print or change terminal characteristics.
-a, --all
print all current settings in human-readable form
-g, --save
print all current settings in a stty-readable form
-F, --file=DEVICE
open and use the specified DEVICE instead of stdin
--help
display this help and exit
--version
output version information and exit
Optional - before SETTING indicates negation. An * marks
non-POSIX settings. The underlying system defines which
settings are available.
Local settings:
[-]echo
echo input characters
您可以使用「-s」讀命令來隱藏用戶輸入的選項。你
echo -n "Password:"
read -s password
if [ $password != "..." ]
then
exit 1; # exit as password mismatched #
fi
也可以使用'SSTY -echo',如果你想從終端隱藏打印。並使用「SSTY回聲」
恢復終端設置,但我認爲從用戶獲得密碼輸入「讀-s密碼」是綽綽有餘。
的可能重複[如何從shell腳本中的密碼,而不回顯(http://stackoverflow.com/questions/3980668/how-to-get-a-password-from-a-shell-script-沒有回聲) – javaPlease42 2015-06-25 23:02:38