如何在不回显的情况下从 shell 脚本获取密码 [英] How to get a password from a shell script without echoing
问题描述
我有一个脚本可以自动执行需要访问密码保护系统的过程.该系统通过接受用户密码作为参数的命令行程序进行访问.
I have a script that automates a process that needs access to a password protected system. The system is accessed via a command-line program that accepts the user password as an argument.
我想提示用户输入他们的密码,将其分配给一个 shell 变量,然后使用该变量来构建访问程序的命令行(这当然会产生我将处理的流输出).
I would like to prompt the user to type in their password, assign it to a shell variable, and then use that variable to construct the command line of the accessing program (which will of course produce stream output that I will process).
我是 Bourne/Bash 中相当称职的 shell 程序员,但我不知道如何接受用户输入而不让它回显到终端(或者可能使用*"字符回显).
I am a reasonably competent shell programmer in Bourne/Bash, but I don't know how to accept the user input without having it echo to the terminal (or maybe having it echoed using '*' characters).
有人可以帮忙吗?
推荐答案
这是另一种方法:
#!/bin/bash
# Read Password
echo -n Password:
read -s password
echo
# Run Command
echo $password
read -s
将为您关闭回显.只需将最后一行的 echo
替换为您要运行的命令即可.
The read -s
will turn off echo for you. Just replace the echo
on the last line with the command you want to run.
这篇关于如何在不回显的情况下从 shell 脚本获取密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!