Insert data in bash prompt with script [duplicate] - bash

This question already has answers here:
Read user input inside a loop
(6 answers)
Kerberos kinit enter password without prompt [closed]
(6 answers)
Closed 5 years ago.
I created a bash script that perform some operations inside a while loop.
In one command, the console asks for an input and I have no idea how to provide this input in order to continue with my script.
while read line;
do
string_array=($line)
username=${string_array[0]}
password=${string_array[1]}
kinit $username
===> here I need to enter the $password and press "ENTER" to continue
done <myfile
Any suggestion?

Change your main while-loop to read from a different file descriptor than stdin and use read to read from stdin and use -s to suppress text from showing up in console.
while read -u 3 line; do
# Your rest of the code
read -s -p "Enter password: " password
done 3<myfile

Related

How to automate OPENVPN login [duplicate]

This question already has answers here:
Send string to stdin
(6 answers)
Closed 2 years ago.
I created a very simple bash script. The first line of the script after #!/bin/bash accesses my vpn service using an OPENVPN file provided by the VPN Vendor. It works as expected and then waits for data entry of my username and once entered expects my password.
I used the echo command to provide the required responses but they never occur. Open VPN just sits there waiting for my username. If I hit enter twice then OPENVPN terminates and my script completes by echoing the username and password.
#!/bin/bash
sudo openvpn /etc/openvpn/us-dtw.prod.xxxxxxx.com_udp.ovpn
echo $'\r'
printf " xf3Z3ZY6xxxxxxxxEuRDmh"
echo $'\r'
echo " nvn7B5kxxxxhxxJstRU"
What could be causing my script to hang and not execute the echo commands until OPENVPN terminates?
Maybe there is a way to pipe the text but I am really new to this.
Found an example that works correctly to automate the VPN logon process: https://help.anonine.com/support/solutions/articles/5000613671-how-do-i-save-my-username-password-in-openvpn-for-automatic-login-
openvpn will by default be using the TTY to get input, not standard input.
Have you looked at the --askpass option to openvpn?

Shell program behaves differently when read from a file vs. pipe [duplicate]

This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Closed 2 years ago.
I made a bash script for my personal usage which sets up selenium webdriver with appropriate options . Here is its raw link - https://del.dog/raw/edivamubos
If i execute this script using curl after writing it to a file first like..
curl https://del.dog/raw/edivamubos -o test.sh && \
chmod u+x test.sh && \
bash test.sh
The script works perfectly as its intended to work
But usually i like to execute scripts directly using curl , so when i do..
curl https://del.dog/raw/edivamubos | bash
The script works very weirdly , it keeps repeating line 22,23 and 29 infinitely on loop. i couldnt beleive it as first so i tested this 3,4 times and can confirm it.
Now
what is the reason for same script acting differently in both cases ?
How do i fix it ( ie make it work correctly even after executing it directly without writing to a file )
Edit -
If someone want they can quickly test this in google colab ( in case someone intending to test but don't want to install any packages locally ) . I am mentioning this thing because you won't be able to reproduce this properly in any bash IDE.
When you pipe the script to bash, this command (line 24):
read -p "Enter your input : " input
reads the next line (i.e. line 25, case $input in) because bash's stdin is connected to curl's stdout, and read reads from the same descriptor as bash.
To avoid that, the developer can change the script so that all input is read from /dev/tty (i.e. the controlling terminal). E.g.:
read -p 'prompt' input </dev/tty
Or the user can use one of the below, so that read reads from the terminal, not the descriptor it was read from.
bash -c "$(curl link)"
bash <(curl link)

Is it possible to partially auto-fill inputs in bash? [duplicate]

This question already has answers here:
Pre-filling a prompt in Bash
(2 answers)
Closed 5 years ago.
Let's say I need a user input in a bash shell script. For example:
What is your name: |
(| is the cursor)
Instead of a blank entry space, is it possible to auto-fill the input for the user, then he or she could continue to change the input if needed and press enter?
What is your name: Mathew|
What is your name: Mat|
What is your name: Matthew|
Yes, using read:
read -e -p "What is your name: " -i "Mathew" RESULT
echo $RESULT
Will give you:
Enter your name: Mathew
And you can edit or just press enter and $RESULT will hold the value

Bash: Set up a new command on a new line without executing [duplicate]

This question already has an answer here:
How to prefill command line input
(1 answer)
Closed 6 years ago.
I'm trying to write a BASH script to output a partially completed command which I can then add parameters to, hit ENTER and then run. I want this to be implemented completely in BASH.
e.g.
~> ./test.sh
~> ls -al <CURSOR POSITION HERE>
The only variable I've found that's close is the PROMPT_COMMAND variable, which when set inside test.sh to 'ls -al', will then immediately execute it once the script has exited.
Is there a way to stop the immediate execution, so I can add, say, *.log?
How about
read -e -p"$PWD> " -i"ls -al " cmd; eval "$cmd"

SSH while read line loop without using remote commands [duplicate]

This question already has answers here:
Looping through lines in a file in bash, without using stdin
(3 answers)
Execute a command on remote hosts via ssh from inside a bash script
(4 answers)
Closed 8 years ago.
I have a list of IP addresses I need to ssh into, making unique changes to each one. I tried doing
while read -r line; do
ssh -n $line; done < file
but I need to manually run commands and checks on each device. The second I login it kicks me out to the next one. Are there any read line or ssh options that can allow me to do this?
Tell read to use a different FD, then you can remove the -n from ssh.
while read -u 3 ...
do
...
done 3< file

Resources