script sh :命令等待来自键盘的输入,但脚本键入输入

script sh : command wait an input from keyboard, but script type the input

我有一个命令需要从键盘输入但不能直接从命令行获取它,比如 sudo,我忘记了将已知变量传递给的语法一个 'terminal input: [sudo] password for cl-r:'

#/bin/bash (or ksh)
.....
VARIABLE_NEEDED=
mycommand_wait_and_needs_input_from_keyboard [?? magic-instruction_beginning ??] # command prompt for input
VARIABLE_NEEDED
[?? magic-instruction_end ??]
# ... command executed
[ $? != 0 ] && ...

也许你的意思是:

echo -n "$VARIABLE_NEEDED" | mycommand_wait_and_needs_input_from_keyboard

看看line 7 of my dotfiles。您可以使用 sudo 权限在 bash 脚本中调用 sudo -v 以 运行 它。