Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to auto answer Yes or No to Linux-Unix commands.


A lot of time Linux/Unix commands or within shell script we need to provide the confirmation input as Yes or No. This situation of manual input provision can be avoided. In this article we will be discussing how we can auto answer Yes or No to Linux-Unix commands to make sysadmins life easier.auto answer Yes or No to Linux-Unix commands


How to auto answer Yes or No to Linux-Unix commands.

Method 1: echoing yes or no

You might have used this method by echoing yes or no before the actual command or script and placing the pipe “|”in between the two commands. To elaborate this methods lets take an example.

Lets say you have got five txt files in your directory “/tmp/mann” as below.

[[email protected] mann]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 24 07:45 1.txt
-rw-r--r--. 1 root root 0 Jun 24 07:45 2.txt
-rw-r--r--. 1 root root 0 Jun 24 07:45 3.txt
-rw-r--r--. 1 root root 0 Jun 24 07:45 4.txt
-rw-r--r--. 1 root root 0 Jun 24 07:45 5.txt
[[email protected] mann]# pwd
/tmp/mann
[[email protected] mann]#

Now you want to remove or delete only one file “1.txt” within this directory. Answer is simple, just fire rm command . However this command ask for the yes or no conformation before proceeding for deletion of file. Like below:

[[email protected] mann]# rm 1.txt
rm: remove regular empty file ‘1.txt’? y

In the above method we have provided “y” input to rm command. This can be avoided by echoing y prior to rm command as below:

[[email protected] mann]# echo y|rm 1.txt
rm: remove regular empty file ‘1.txt’? [[email protected] mann]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 24 08:09 2.txt
-rw-r--r--. 1 root root 0 Jun 24 08:09 3.txt
-rw-r--r--. 1 root root 0 Jun 24 08:09 4.txt
-rw-r--r--. 1 root root 0 Jun 24 08:09 5.txt
[[email protected] mann]#

auto answer Yes or No to Linux-Unix commands

Yippee we given automated answer to command but there is still a problem. This echo command cannot be used for answering continuous yes or no answer to commands. In this situation you need use next method.

Method 2: Yes command. 

Yes command is an another option we have to auto answer Yes or No to Linux-Unix commands.

Basically what yes command does that , it continuously outputs strings passed on to it or even it pass Yes string alone. Hence we can use this opportunity to auto answer Yes or No to Linux/Unix commands/scripts.  Lets take an example. We have got five files as previously and we want to delete all those files without answering the confirmation question Yes or No. Then try below method which uses “Yes” command.

[[email protected] mann]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 24 08:03 1.txt
-rw-r--r--. 1 root root 0 Jun 24 08:03 2.txt
-rw-r--r--. 1 root root 0 Jun 24 08:03 3.txt
-rw-r--r--. 1 root root 0 Jun 24 08:03 4.txt
-rw-r--r--. 1 root root 0 Jun 24 08:03 5.txt
[[email protected] mann]# yes |rm *
rm: remove regular empty file ‘1.txt’? rm: remove regular empty file ‘2.txt’? rm: remove regular empty file ‘3.txt’? rm: remove regular empty file ‘4.txt’? rm: remove regular empty file ‘5.txt’? 
[[email protected] mann]# ll
total 0
[[email protected] mann]#

Here what happened is that, Yes command given continuous answer as Yes to next rm command. Which results into deletion of all the files without manual intervention. This cannot be achieved with echo yes method since it gives input as Yes for single time only!!!

Method 3: Yes command with other input.

Lets say you don’t want to answers as Yes instead now this time you want to answer as No for all the confirmations asked by command. Then follow below procedure. Here we have answered as No to all the answers.  Hence no files have been deleted by the system.

[[email protected] mann]# yes n |rm *
rm: remove regular empty file ‘1.txt’? rm: remove regular empty file ‘2.txt’? rm: remove regular empty file ‘3.txt’? rm: remove regular empty file ‘4.txt’? rm: remove regular empty file ‘5.txt’? 
[[email protected] mann]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 24 08:09 1.txt
-rw-r--r--. 1 root root 0 Jun 24 08:09 2.txt
-rw-r--r--. 1 root root 0 Jun 24 08:09 3.txt
-rw-r--r--. 1 root root 0 Jun 24 08:09 4.txt
-rw-r--r--. 1 root root 0 Jun 24 08:09 5.txt
[[email protected] mann]#

Even you can use below command instead of above one.

# yes no |rm *

Conclusion:

In this way we can make use of Yes command to auto answer Yes or No to Linux-Unix commands. This very much helps sysadmin to execute commands in uninterrupted form, Which makes their life easy and comfortable.

Get your free copy of shell scripting guide in PDF format!!!!

Download This Book: Click Here!!

The post How to auto answer Yes or No to Linux-Unix commands. appeared first on UX Techno.



This post first appeared on UxTechno, please read the originial post: here

Share the post

How to auto answer Yes or No to Linux-Unix commands.

×

Subscribe to Uxtechno

Get updates delivered right to your inbox!

Thank you for your subscription

×