How to assign the Pattern Search string as Input Variable

guys,

I need to know how to assing pattern matched string as an input command variable. Here it goes'

My script is something like this.

./routing.sh <Server> <enable|disable>

## This Script takes an input <Server> variable from this line of the script ##

echo $1 | egrep "^(pserv|dserv)[0-9][0-9][0-9][0-9]\-ra$" > /dev/null 2>&1


But this script always display " request authenticated " even though the server doesn't exists in the network just by matching the 1st 5 strings of the server name pserv & dserv and irrespective of the numerical.

But now my idea is that the script should read the host file & match the pattern and should take that existing host as an input <Server> variable and should display the " Request Authenticated " instead of shooting the bullet in the dark .

Sample:

./routing pserv0000 disable

Request Authenticated ..

Server will be Disbale from traffic in few seconds 

Pinging the host

Error: Node Doesn't Exist

From the above example, the server pserv0000 does not exists, but still its showing " Request Authenticated ".

Basically I want script to read the /ect/hosts file & should match the pattern or else it should throw out " Invalid Request '

Can you guys give me an hint !! How to go about it & replace the above line.

:slight_smile:

You can try something dirty like this:

fgrep "$1" /etc/hosts > /dev/null || {
  echo Invalid Request
  exit 1
  }

If your grep implementation supports the -q switch you don't have to redirect stdout. With POSIX implementation you could use -F instead of fgrep.
You could use a regex and be much more specific.