System Administration

Re: Shell Script in non-prompt mode

 
SOLVED
Go to solution
Sammy_2
Super Advisor

Shell Script in non-prompt mode


How do I get place the script in non-interactive mode so that it automatically answers "y" and hits carriage return after each prompt instead of waiting for a user input. Please look the script below.

===========================================
for i in `vgdisplay -v vgtest| egrep "LV Name"|awk '{print $NF}'`
do
lvreduce $i
done
==============================================

I get the following prompt after before LV removal.
=====================
The logical volume "/dev/vgtest/lvol1" is not empty;
do you really want to delete the logical volume (y/n) :

========================
good judgement comes from experience and experience comes from bad judgement.
7 REPLIES 7
Patrick Wallek
Honored Contributor

Re: Shell Script in non-prompt mode

use 'lvreduce -f'.
Tom Maloy
Respected Contributor

Re: Shell Script in non-prompt mode

It's not the shell script, it's each of the individual commands. So you will need to use the "-f" option for lvreduce, and other options (if available) for other commands. Or else use "expect" or pipe the required characters into the command.

Tom
Carpe diem!
Darrell Allen
Honored Contributor
Solution

Re: Shell Script in non-prompt mode

Hi,

For this command, there's the -f option as already pointed out. When the command doesn't have an option, most can be scripted by using "here-document" syntax. You simply redirect standard input from the script. Example:

command <y

something
something else
n
EOF

In this example, "y" is passed as input the first time "command" expects input. The second time, a null response is input (the blank line is just as if you entered CR from the keyboard). The third time "something" is input. And so on...

For here-documents, there are to be no leading spaces for any input line nor the ending "word" (EOF in this example). You can use leading tabs if you specify the here-document by using <<- instead of <<. Also, EOF isn't special. You can use any word. See man sh-posix for more info.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Jean-Louis Phelix
Honored Contributor

Re: Shell Script in non-prompt mode

Hi,

Just for information because you've got some good answers already (-f option of lvreduce and redirecting input using <<), but there is also a funny tool that you can use when you need a lot of 'y' answer. It's 'yes', which sends 'y' followed by a linefeed repeatedly (or any other string if you want). So just pipe it to your script :

yes | rm -i a b

will answer yes to all suppress confirmation (I know that in this case 'rm a b' would be better :^) ).

Or

yes good | foo

will answer good to all foo inputs.

Regards.
It works for me (© Bill McNAMARA ...)
Steven E. Protter
Exalted Contributor

Re: Shell Script in non-prompt mode

from the man page.

lvreduce cannot be performed if the volume group is activated in shared mode.

Even if you get feed the y to the program if you try it in certain circumstances it will squawk.

more
lvreduce asks for confirmation before deallocating logical extents if
the -f option is omitted.



redundant post? Perhaps.

Steve
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sammy_2
Super Advisor

Re: Shell Script in non-prompt mode

Thanks All.
Jean- your suggestion works on rm command
but when I do a lvremove on LV
y | lvremove /dev/vgmass/lvol1
it gives me "Bad Input Parameter"
Darrell-yours worked perfectly with the EOF script
Steve, Tom, Pat, thanks to all with lvreduce option but I was looking for an option to be used with other commands as well.
good judgement comes from experience and experience comes from bad judgement.
Jean-Louis Phelix
Honored Contributor

Re: Shell Script in non-prompt mode

hi,

Strange, I give you the execution on my system :

/.root# lvcreate -L 12 vg00
Logical volume "/dev/vg00/lvol13" has been successfully created with character device "/dev/vg00/rlvol13".
Logical volume "/dev/vg00/lvol13" has been successfully extended.
Volume Group configuration for /dev/vg00 has been saved in /etc/lvmconf/vg00.conf
/.root# yes | lvremove /dev/vg00/lvol13
The logical volume "/dev/vg00/lvol13" is not empty;
do you really want to delete the logical volume (y/n) : Logical volume "/dev/vg00/lvol13" has been successfully removed.
Volume Group configuration for /dev/vg00 has been saved in /etc/lvmconf/vg00.conf

So it seems to work, but I use 'yes', not 'y'. Anyway it was just for fun because in your case I would also have redirected input using << :^)

Regards.
It works for me (© Bill McNAMARA ...)