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

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

Related

Insert data in bash prompt with script [duplicate]

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

How to pass consecutive multiple arguments and enter to a command inside shell script?

I have created a simple shell script. In this script one command require user input as 1 1 1 ENTER ENTER ENTER ENTER etc.
How can i pass these user inputs to script?
Regards,
Ankit
If you want to pass three lines consisting of the string "1" followed by an arbitrary number of blank lines to the command cmd, you can do:
yes "" | sed -e 1,3s/^/1/ | cmd
What shell are you using?
Typically ,You can pass user input using read.
Here is an example using bash.
#!/bin/bash
# Ask User to Input Data
echo "Enter your Number"
#Store user input to a variable
read userNumber
#Process user input. In this case display it back
echo "You entered $userNumber"

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"

Writing shell script using another shell script

I'm trying to automate running of a shell script that would take some user inputs at various points of its execution.
The basic logic that I've in my mind is copied below, but this is only for one input. I wanna run it recursively until the shell prompt is received after the original script completes its execution. I said recursively because, the question that prompts for an input and the input itself will be the same all the time.
#!/usr/bin/expect
spawn new.sh $1
expect "Please enter input:"
send "my_input"
Sharing any short-cut/simple method to achieve this will be highly appreciated.
You don't need expect to do this - read can read from a pipe as well as from user input, so you can pass the input through a pipe to your script. Example script:
#!/bin/bash
read -p "Please enter input: " input
echo "Input: $input"
Running the script prompts for input as normal, but if you pipe to it:
$ echo "Hello" | sh my_script.sh
Input: Hello
You said that your input is always the same - if so, then you can use yes (which just prints a given string over and over) to pass your script the input repeatedly:
yes "My input" | sh my_script.sh
This would run my_script.sh, any read commands within the script will read "My input".

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