I have a program which accepts 2 prompts (y/n). For example:
stopprogram
do you want to stop the program (Y/N)? y
do you want to send an email to the admin about it (Y/N)? y
Now, I'd like to automate that using the 'at' command. the following works on Solaris but not on Linux RHEL:
at now +5 minutes << EOF
> for i in {1..2}
> do
> echo 'y'
> done | stopprogram
> EOF
commands will be executed using /usr/bin/bash
...
...
Any idea? Thanks!
Your problem may be because of the space between << and EOF.
Note that there is a special program yes for repeatedly outputting a line composed of all of its arguments. By default it outputs 'y'. It was created specially for forcing a scripted flow through those prompts.
Thus the short version of your command will look like this:
at now +5 minutes <<EOF
yes | stopprogram
EOF
I found the solution. This will work:
at now+5 min <<EOF
bash -l -c 'yes | stopprogram'
EOF
That's it!
Related
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
I ve a specific shell command , which runs on ubuntu terminal
So while running in a terminal, i'm invoking it like that :
mycommand
while running , under terminal , it starts to ask for some "prompts" (for configuration) which i should tape , like this :
and then , another time with another ask for "confirmation"
, and then for almost 10 other times for different config confirmations
where i'm taping diiferents responses
So my purpose is how to be able to do it in once , in only one-line command ??
i would try this :
mycommand "yes" "yes "no" ..... "yes"
But that's KO .
Suggestions ??
If you know all the required inputs, you can create a file with the answers:
answers.in:
yes
yes
no
yes
and then
mycommand << answers.in
If mycommand is reading from stdin and not from the tty, then you can get away with echoing responses into the command. This is in fact one of the main reasons for the existence of the yes command:
yes | mycommand
will respond 'y' to all of the prompts.
yes foo | mycommand
will respond with 'foo' to all of the prompts. If you want to answer 'yes' to some and 'no' to others, you can get creative and do things like:
yes yes | sed -e 3,5s/yes/no/ -e 10q | mycommand
(The above will respond "yes" to the first 2 prompts, then "no" to prompts 3 thru 5, and then "yes" to prompts 6 thru 10).
You can use print as '#William Pursell' wrote. I tend to use echo out of habit.
$ cat /tmp/x.sh
#!/bin/sh
read -p "alpha: " alpha
read -p "beta: " beta
read -p "gamma: " gamma
echo "retrieved:"
echo "\$alpha: $alpha"
echo "\$beta: $beta"
echo "\$gamma: $gamma"
$ echo "a\nb\nc" | /tmp/x.sh
retrieved:
$alpha: a
$beta: b
$gamma: c
or, more intuitive to me:
$ (echo "a"; echo "multiple words"; echo optional quotes) | /tmp/x.sh
retrieved:
$alpha: a
$beta: multiple words
$gamma: optional quotes
And, as '#Jean-Loup Sabatier' mentioned. expect is good to add logic if your input needs to change based on some logic, but that's a different question.
Using at to schedule a systems message. I need to run it in a script, so the first option which requires ctrl+d is not viable. Also note, in the first option, the pipe fails UNLESS I add a nonsense echo statement below it.
first option, works but cannot use:
$ at now
> cat file.txt | write user pts/0
> echo "nonsense"
>[ctrl+d]
second option, which needs to work:
$ at now << 'END_AT'
> cat file.txt | write user pts/0
> END_AT
Please note that with or without the nonsense echo in the second option, it fails to cat the contents of the text file. I need this second option to properly display, what am I doing wrong here??
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
I want to ask if is possible to combine linux command and <
sendmail -S "lalalal" -f "dailaakak" -au "kakakak" <<EOF
>lalal:lalal
>opp:ttt
>ggg:zzz
EOF
I want to have something like that sendmail -S "lalalal" -f "dailaakak" -au "kakakak" <<EOF; lalal:lalal; opp:ttt; ggg:zzz; EOF
I need to use that not in bash script
If it has to be in one line without newlines use that:
echo -e "lalal:lalal\nopp:ttt\nggg:zzz" | sendmail -S "lalalal" -f "dailaakak" -au "kakakak"
echo -n interpretes escapes characters such as \n as a newline.
If you are asking whether you can use the << EOF in an interactive shell then the answer is yes, you can.
Note this functionality is called here document and that there can be any word instead of EOF. For example:
$ cat - << someword
> Here you
> can
> write text with as many
> newlines as you want.
> someword
Here you
can
write text with as many
newlines as you want.
(cat - prints whatever it receives on stdin)
For more information on here documents you can read for example this: http://tldp.org/LDP/abs/html/here-docs.html
I have tried and succeeded but it's messy. EOF simply does not like to accept substituted new lines for some reason so it needs to be put in another format. Now I'm sure this could be achieved with an expect script one one line but the below is what I have made and works.
echo "ssh localhost `printf "<< EOF\necho "Working!" >> /tmp/myfile \nEOF\n"`" > file.sh; chmod770 file.sh; ./file.sh
printf "<< EOF\necho Test! >> /tmp/myfile \nEOF\n" | xargs ssh localhost
Please ensure chmod file permissions are suitable for your own work case! Putting it into an environment variable instead of a file is also likely to work.