-1

I've a command that needs input from keyboard and cannot get it directly from command line, like sudo, and I've forgotten the syntax to pass a known variable to a 'terminal input: [sudo] password for cl-r:'

#/bin/bash (or ksh)
.....
VARIABLE_NEEDED=$1
mycommand_wait_and_needs_input_from_keyboard [?? magic-instruction_beginning ??] # command prompt for input
VARIABLE_NEEDED
[?? magic-instruction_end ??]
# ... command executed
[ $? != 0 ] && ...
Jens
  • 69,818
  • 15
  • 125
  • 179
cl-r
  • 1,264
  • 1
  • 12
  • 26
  • 1
    possible duplicate of [How do I prompt for input in a Linux shell script?](http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script) – m.s. Jul 18 '15 at 15:16
  • @m.s. I've modified my answer : It not a read problem from a script, but to pass an instruction to a waiting command. – cl-r Jul 18 '15 at 15:24

2 Answers2

1

Maybe you mean the following:

echo -n "$VARIABLE_NEEDED" | mycommand_wait_and_needs_input_from_keyboard
yolenoyer
  • 8,797
  • 2
  • 27
  • 61
  • It's a right way to pass input data to another command (`-n` is optionnal). When you say "it does not work", what's happening exactly please? – yolenoyer Jul 18 '15 at 17:23
  • I tried with sudo `echo -n mypass | sudo -i` and I got `[sudo] password for cl-r :` and not the root prompt. – cl-r Jul 18 '15 at 20:10
  • 1
    Ok... Due to security reasons, there's no way (to my knowledge) to enter a `sudo` password with a bash command. You can tweak `sudo` in many ways, see `man sudo`, but not pass the password with the help of a variable. – yolenoyer Jul 18 '15 at 20:22
  • I'm confuse, I've forgotten that it is done by an *expect script* I used many years ago before using Java fork possiblities. Cf. : https://en.wikipedia.org/wiki/Expect . Undirectly you help me to solve my problem ! :) – cl-r Jul 18 '15 at 21:11
  • Expect tutorial and Autoexpect tool : http://www.admin-magazine.com/Articles/Automating-with-Expect-Scripts – cl-r Jul 18 '15 at 21:45
0

Take a look at line 7 of my dotfiles. You can invoke sudo -v in a bash script to run it with sudo permissions.

AJ.
  • 1,248
  • 10
  • 27
  • Interesting, but my problem is keybord configuration at launch time is not the keyboard I usually use, so special characters are not on the key face : *QWERTY* vs *AZERTY* vs *enhanced* and so on... – cl-r Jul 19 '15 at 09:38