A method in the shell script to convert the input password to * (asterisk) _linux shell

Source: Internet
Author: User

If you need to write a section to interact with the user, and need to input some sensitive information (for example: User password, license, etc.), the direct use of printf+read, the user input information will be displayed on the screen, which is not in line with information security, and the customer experience is not professional, So you need to convert the password entered by the user to * with the following style:

Please input your passwd:1234
Modified to:
Please input your passwd:****

So specifically how to achieve it, please look down ...

#!/bin/sh


GetChar () {

  stty cbreak-echo

  dd if=/dev/tty bs=1 count=1 2>/dev/null stty-cbreak echo
  }


printf "Please input your passwd:" while


:; Do

  ret= ' GetChar '

  if [x$ret = x]; then

    echo

    break

  fi

  str= "$str $ret"

  printf "*"

done


Echo ' Your password is: $str '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.