With a bash script that utilizes system commands; how would you have it input when the COMMAND asks you for something? [duplicate] - linux

This question already has answers here:
Passing arguments to an interactive program non-interactively
(5 answers)
Closed 7 years ago.
So suppose a normal command run in terminal goes like this....
user$ thecommand
Please enter your first name:
>
and then waits for your to type your name... straightforward, but if in a bash script I try and do something like:
#! /bin/bash
echo "What is your name?"
read name
thecommand
how would I have THE SCRIPT enter "$name" in response to "thecommand" instead of having the user manually input it themselves?

you can add input by pipe like this:
echo yourname | ./yourscript
for more inputs you can use printf
printf "input1\ninput2" | ./yourscript
where \n means new line and it will be used like new input.

Run your script like:
./yourscript.sh < file.txt
where file.txt will contain the name.
now your script will look for name from the file(file.txt), in file.txt you can type the names which will act as input for read command.
read command reads on line at a time so if u have more than on read command in your script you should have multiple lines in file.txt file

For complicated cases, for example if your input depends on the output of your command, you may write an "expect" script.
To see how it works you can auto-generate such script interactively
$ autoexpect thecommand
And then run it
$ expect -f script.exp

Related

How to pre-specify a selection when executing a program on Linux [duplicate]

I have a bash script that employs the read command to read arguments to commands interactively, for example yes/no options. Is there a way to call this script in a non-interactive script passing default option values as arguments?
It's not just one option that I have to pass to the interactive script.
Many ways
pipe your input
echo "yes
no
maybe" | your_program
redirect from a file
your_program < answers.txt
use a here document (this can be very readable)
your_program << ANSWERS
yes
no
maybe
ANSWERS
use a here string
your_program <<< $'yes\nno\nmaybe\n'
For more complex tasks there is expect ( http://en.wikipedia.org/wiki/Expect ).
It basically simulates a user, you can code a script how to react to specific program outputs and related stuff.
This also works in cases like ssh that prohibits piping passwords to it.
You can put the data in a file and re-direct it like this:
$ cat file.sh
#!/bin/bash
read x
read y
echo $x
echo $y
Data for the script:
$ cat data.txt
2
3
Executing the script:
$ file.sh < data.txt
2
3
Just want to add one more way. Found it elsewhere, and is quite simple.
Say I want to pass yes for all the prompts at command line for a command "execute_command", Then I would simply pipe yes to it.
yes | execute_command
This will use yes as the answer to all yes/no prompts.
You can also use printf to pipe the input to your script.
var=val
printf "yes\nno\nmaybe\n$var\n" | ./your_script.sh

Execute command substitutions in input read from a file

In shell script how to make script read commands in input file string
Example 1 (script1.sh):
a="google.analytics.account.id=`read a`"
echo $a
Example 2 (script2.sh):
cat script2.sh
a=`head -1 input.txt`
echo $a
Sample input.txt
google.analytics.account.id=`read a`
If I run script1.sh the read command is working fine, but when I am running script2.sh, the read command is not executed, but is printed as part of the output.
So I want script2.sh to have the same output as script1.sh.
Your input.txt contents are effectively executed as a script here; only do this if you entirely trust those contents to run arbitrary commands on your machine. That said:
#!/usr/bin/env bash
# ^^^^- not /bin/sh; needed for $'' and $(<...) syntax.
# generate a random sigil that's unlikely to exist inside your script.txt
# maybe even sigil="EOF-$(uuidgen)" if you're guaranteed to have it.
sigil="EOF-025CAF93-9479-4EDE-97D9-483A3D5472F3"
# generate a shell script which includes your input file as a heredoc
script="cat <<$sigil"$'\n'"$(<input.txt)"$'\n'"$sigil"
# run that script
eval "$script"
In script1.sh the first line is evaluated, therefore the read a is executed and replaced in the string.
In script 2.sh the first line is evaluated, therefore the resulting string from execution of head is put into the variable a.
There is no re-evaluation done on the resulting string. If you add the evaluation with eval $a and the first line in input.txt is exactly as the first line of script1.sh (actually the a="..." is missing) then you might get the same result. The heredoc, as CharlesDuffy suggested, seems more accurate.

Automate "Press enter to continue" in shell script [duplicate]

I have a bash script that employs the read command to read arguments to commands interactively, for example yes/no options. Is there a way to call this script in a non-interactive script passing default option values as arguments?
It's not just one option that I have to pass to the interactive script.
Many ways
pipe your input
echo "yes
no
maybe" | your_program
redirect from a file
your_program < answers.txt
use a here document (this can be very readable)
your_program << ANSWERS
yes
no
maybe
ANSWERS
use a here string
your_program <<< $'yes\nno\nmaybe\n'
For more complex tasks there is expect ( http://en.wikipedia.org/wiki/Expect ).
It basically simulates a user, you can code a script how to react to specific program outputs and related stuff.
This also works in cases like ssh that prohibits piping passwords to it.
You can put the data in a file and re-direct it like this:
$ cat file.sh
#!/bin/bash
read x
read y
echo $x
echo $y
Data for the script:
$ cat data.txt
2
3
Executing the script:
$ file.sh < data.txt
2
3
Just want to add one more way. Found it elsewhere, and is quite simple.
Say I want to pass yes for all the prompts at command line for a command "execute_command", Then I would simply pipe yes to it.
yes | execute_command
This will use yes as the answer to all yes/no prompts.
You can also use printf to pipe the input to your script.
var=val
printf "yes\nno\nmaybe\n$var\n" | ./your_script.sh

Linux Bash Script that can take Unlimitd Arguments [duplicate]

This question already has answers here:
How do I use a variable argument number in a bash script?
(3 answers)
Closed 6 years ago.
What's going on everyone? I have this assignment in my Linux Operating System class and im having a little trouble. It asks me to,
Write a Bash shell script called chkread that takes an unlimited number of
arguments that all represent file names.
I have come up with this so far, but i don't think it's exactly what the professor is looking for.
~$ cat MY_SCRIPT
#!/bin/bash
echo ${unlimited arguments}
~$ bash MY_SCRIPT cat dog horse
cat dog horse
You could use something like this to handle multiple arguments in a bash script.
#!/bin/bash
file_names=("$#")
for name in "${file_names[#]}"; do
echo "$name"
done
And then when you call the script:
bash chkread.sh file1 file2 file3 file4
The script will print them to output:
file1
file2
file3
file4
But this is just an example. Inside the script you can do with them whatever you need to do.
Use $*. That will give you everything. For example:
#!/bin/bash
file_names=$*
echo $file_names
Output:
jbanks#efsappdev1:~$ x.sh `ls *.sql`
current.sql goop.sql latest.sql long.sql report.sql
jbanks#efsappdev1:~$ x.sh one two three
one two three

Do a complete flux of work on bash script

I'm trying to automate a proces which I have to do over and over again in which I have to parse the output from a shell function, look for 5 different things, and then put them on a file
I know I can match patterns with grep however I don't know how to store the result on a variable so I can use it after :(
I also have to parse this very same output to get the other 5 values
I have no idea on how to use the same output for the 5 grep's i need to do and then store it to 5 different variables for after use
I know i have to create a nice and tidy .sh but I don't know how to do this
Currently im trying this
#!/bin/bash
data=$(cat file)
lol=$(echo data|grep red)
echo $lol
not working , any ideas?
you should show some examples of what you want to do next time..
assuming you shell function is called func1
func1(){
echo "things i want to get are here"
}
func1 | grep -E "things|want|are|here|get" > outputfile.txt
Update:
your code
#!/bin/bash
data=$(cat file)
lol=$(echo data|grep red)
echo $lol
practically just means this
lol=$(grep "red" file)
or
lol=$(awk '/red/' file)
also, if you are considering using bash, this is one way you can do it
while read -r myline
do
case "$myline" in
*"red"* ) echo "$myline" >> output.txt
esac
done <file
You can use the following syntax:
VAR=$(grep foo bar)
or alternatively:
VAR=`grep foo bar`
The easiest thing to do would be to redirect the output of the function to a file. You can then run multiple greps on it and only delete the file once you are done with it.
To save the output, you want to use command substitution. This runs a command and then converts the output into command line parameter. Combined with variable assignment you get:
variable=$(grep expression file)
Your second line is wrong. Change it to this:
lol=$(echo "$data"|grep red)
use egrep istead of grep.
variable=$(egrep "exp1|exp2|exp3|exp4|exp5" file)

Resources