2011-04-12 7 views
13

나는 Bourne 쉘 스크립트를 작성하고이 같은 암호를 입력해야 해요 : 분명히터미널의 반향을 끄려면 어떻게합니까?

echo -n 'Password: ' 
read password 

, 나는 터미널에 에코되는 비밀번호를하지 않으을, 그래서이 기간 동안 에코를 끄려면 읽습니다. stty을 사용하여이 작업을 수행 할 수있는 방법이 있다는 것을 알고 있지만 맨페를 읽으면서 커뮤니티의 이점에 대해 질문 할 것입니다. ;)

+1

가능한 복제본 [에코없이 셸 스크립트에서 암호를 얻는 방법] (http://stackoverflow.com/questions/3980668/how-to-get-a-password-from-a-shell-script- without-echoing) – javaPlease42

답변

31
stty_orig=`stty -g` 
stty -echo 
echo 'hidden section' 
stty $stty_orig 
+0

메모리가 작동하는 경우 이것은 확실히 이전에 수행 한 방식입니다. 호기심에서 벗어난이 것은'stty -echo와는 다르다. foo를 읽는다; stty echo'? –

+2

예, 이미 스위치가 꺼져있는 경우에 대비하여 에코가 꺼진 채로 있습니다. – ak2

+0

@ ak2 : 감사합니다. 완벽한 말이됩니다. –

11

read -s password은 내 리눅스 박스에서 작동합니다.

+2

이것은 유용한 정보입니다.하지만 -s는 Bash 확장 기능인데,'read -s foo'가 생성합니다. Dash (Ubuntu가'/ bin/sh '와 심볼릭 링크를하는 Bourne 쉘)에서'read : 1 : Illegal option -s'을 사용합니다. –

0

Bourne 쉘 스크립트 : 자세한 내용은

#!/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 
0

명령의 '-s'옵션을 사용하여 사용자 입력을 숨길 수 있습니다. 인쇄 터미널에서 숨기려면

echo -n "Password:" 
read -s password 
if [ $password != "..." ] 
then 
     exit 1; # exit as password mismatched # 
fi 

또한 당신은'ssty의 -echo'를 사용할 수 있습니다. 그리고이 충분하다 '-s 비밀번호를 읽기'"ssty 에코"

를 사용하여 터미널 설정을 복원하지만 사용자 에서 비밀번호 입력을 얻기 위해 생각한다.

관련 문제