Friday, August 5, 2022
HomeNetworkingUtilizing the sure command to automate responses

Utilizing the sure command to automate responses


One of many extra uncommon Linux instructions is called “sure”. It’s a quite simple device meant that can assist you keep away from having to reply a number of questions that is perhaps requested once you run a script or a program that wants a sequence of responses to do its work.

If you happen to kind “sure” by itself on the command immediate, your display screen goes to start out filling up with the simply the letter “y” (one per line) till you hit control-C to cease it. It’s additionally extremely quick. Not like what you see displayed under, sure will possible spit out greater than 1,000,000 y’s within the time it possible takes you to achieve down and press control-C. Happily, that’s not all that this command can do.

$ sure
y
y
y
^C

If you happen to like, you possibly can pipe the output of the sure command to the head command to see a small pattern of its output.

$ sure | head -3
y
y
y

As soon as the command receiving the output ends, the sure command ends as nicely.

If you wish to replenish your display screen with “no”, another phrase, or perhaps a full sentence, you are able to do that. The command “sure no” will repeatedly show “no” till you terminate the command, once more with a control-C.

$ sure no | head -3
no
no
no

You might even use a command like “sure I’m filling up my display screen” and watch your display screen filling up with “I’m filling up my display screen”.

There may be one factor that you simply shouldn’t even take into account doing with sure. Right here goes:

WARNING: One factor you actually shouldn’t do is redirect the output of the sure command to a file. It would develop a lot quicker than you’ll possible anticipate.

So, what’s sure meant to do?

Since we’ve lined what sure can do, let’s have a look at the way it’s meant for use. The sure command was constructed to ship solutions to scripts or binaries once you desire to not must reply their questions your self. In actual fact, the sure command makes it simple to run instructions non-interactively and is usually used to automate processes that may run in a single day.

The limitation is that everytime you use the sure command, each line of output goes to be the identical – whether or not “y” (the default), “sure” or one thing else. There isn’t any method which you can get sure to reply “sure” to at least one query and “no” to a different.

If you happen to have been utilizing a script just like the one proven under that asks a sequence of questions and also you need the sure command to reply them for you, you can pipe the output of sure to the script utilizing a command like this:

$ sure | myscript

The script would learn the “y” solutions offered by the sure command and reply accordingly.

$ sure | askme
Able to reply some questions? [y,n]
Did you're employed greater than 8 hours immediately? [y,n]
Then it's possible you'll want some relaxation!
Are you prepared for a nap? [y,n]
Get some good relaxation!

Alternately, you can ask sure to reply all questions with an “n”:

$ sure no | askme
Able to reply some questions? [y,n]
OK, good bye!

The script:

#!/bin/bash

echo "Able to reply some questions? [y,n]"
learn ans

if [ "$ans" != "y" ]; then
   echo "OK, good bye!"
   exit
fi

echo "Did you're employed greater than 8 hours immediately? [y,n]"
learn ans

case $ans in
   y) echo "Then it's possible you'll want some relaxation!";;
   n) echo "OK, however you possibly can take a brief break";;
   *) echo "Avoiding the query?"
esac

echo "Are you prepared for a nap? [y,n]"
learn ans

case $ans in
   y) echo "Get some good relaxation!"; exit;;
   n) echo "OK. Get some good work finished!";;
   *) echo "Too tough a query?";;
esac

Wrap-up

The sure command is proscribed to repeating the identical response to each query requested by a script or command, but it surely offers a strategy to run instructions with out having to be current to offer the wanted responses.

Be a part of the Community World communities on Fb and LinkedIn to touch upon matters which can be prime of thoughts.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments