How to assign the Pattern Search string as Input Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to assign the Pattern Search string as Input Variable
# 1  
Old 02-11-2010
Debian 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.

Code:
./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:

Code:
./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.

Smilie
# 2  
Old 02-11-2010
Quote:
Originally Posted by raghunsi
[...]

Basically I want script to read the /ect/hosts file & should match the pattern or else it should throw out " Invalid Request '
You can try something dirty like this:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

3. Shell Programming and Scripting

Assign a variable the nth character of a string.

I see a millioin ways to do this with echo, but what I wan to do is assign a variable the "nth" character of an incoming parameter to a ksh script. $1 will be "pia" I need to assign the first character to stmttype. (10 Replies)
Discussion started by: klarue
10 Replies

4. Shell Programming and Scripting

Running a program multiple times to search pattern and assign structure

Hi all, I have a big file (n.txt) with following pattern: ATOM 1 N SER A 1 122.392 152.261 138.190 1.00 0.00 N ATOM 2 CA SER A 1 122.726 151.241 139.183 1.00 0.00 C TER ENDMDL ATOM 1 N SER A 1 114.207 142.287 135.439 1.00 0.00 ... (3 Replies)
Discussion started by: bioinfo
3 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

need to assign a string to a variable

Hello Experts, In my script i am using the below syntax /usr/bin/head -1 /opt/chumma.txt | /usr/bin/cut -d " " -f3 output of this one is --> Monday I want to store this String in a variable (i.e) j or k... Give me some idea experts. Thanks in advance. (7 Replies)
Discussion started by: natraj005
7 Replies

7. Shell Programming and Scripting

Bash assign string to variable

Hi ,I am trying to assign string to variable ,but it doesn't work Also could you show me different ways to use grep,(I am trying to get the first,second and first column form file,and I am counting the chars) let name=`grep "$id" product | cut -c6-20` (25 Replies)
Discussion started by: lio123
25 Replies

8. Shell Programming and Scripting

read and assign each character from the string to a variable

How... can I read input by a user character by cahracter. And assign each character from the string to a variable? Any help would be greatly appreciated! Thank you! (1 Reply)
Discussion started by: Tek-E
1 Replies

9. UNIX for Dummies Questions & Answers

search file for word, then assign to variable

Hi all, Trying to search a file for one word only, then assign that word to a variable. Not sure if this is a grep or awk (or either) function. Should be a simple operation. Example: This file contains the string "COMPLETE". I would like to pull that word out and assign it to a... (2 Replies)
Discussion started by: dejit
2 Replies

10. Shell Programming and Scripting

How to assign variable from a string having blanks in between

Hi All, I am new to bash scripting. I need your help to removing spaces from a string and assign them to different variables. Iwant to call script with one command line argument which is file name which containes different attributes their type and different values eg ... (1 Reply)
Discussion started by: flextronics
1 Replies
Login or Register to Ask a Question