LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   match number (http://www.linuxquestions.org/questions/linux-newbie-8/match-number-864917/)

mmhs 02-25-2011 12:05 AM

match number
 
hey guys i gave simple question in bash programming

i want to write shell script that give a port number and find it in /etc/services but i want to find exact match number

for example user enter 4 and script find exact 4 in /etc/services not contain 444 or 45 or 23124 and ...

(just say how can i find match number ) with grep or sed or awk .....

and how can i find exact match string in a file

for example user enter Por and script find just Por string not Port or Port1 or Por-23 or ...


thx

r_s 02-25-2011 12:27 AM

grep has an option for exact matches -w.

mmhs 02-25-2011 01:22 AM

Quote:

Originally Posted by r_s (Post 4270521)
grep has an option for exact matches -w.

:))

grep -w only find word

for example if you have abd and abd-12 and abd.12 if you use grep -w "abd" file it find all of abd abd-12 and abd.12

plz some expert people in bash script help me :(

druuna 02-25-2011 01:55 AM

Hi,

@mmhs: You need to find something that makes the search string unique for the specific infile you are using.

The following will return a specific port from /etc/services:
Code:

grep " 4/" /etc/services
As you can see the wanted string (4) is enclosed by a space (might be a tab, my /etc/services file uses spaces) and a slash, which makes it unique for this specific infile.

Hope this helps.

mmhs 02-25-2011 02:00 AM

Quote:

Originally Posted by druuna (Post 4270573)
Hi,

@mmhs: You need to find something that makes the search string unique for the specific infile you are using.

The following will return a specific port from /etc/services:
Code:

grep " 4/" /etc/services
As you can see the wanted string (4) is enclosed by a space (might be a tab, my /etc/services file uses spaces) and a slash, which makes it unique for this specific infile.

Hope this helps.

:)) check it man u mistake it's wrong 100% it will return nothing

plz some expert man help me and check your solution before post

druuna 02-25-2011 02:08 AM

@mmhs:

Works nicely on my side:

Code:

$ grep " 4/" /etc/services
#                4/tcp    # Unassigned
#                4/udp    # Unassigned

$ grep " 80/" /etc/services
http            80/tcp    # World Wide Web HTTP
http            80/udp    # World Wide Web HTTP

- Did you try changing the space to a tab (as mentioned in my previous post)?
- Are you actually grepping something in /etc/services?
- Did you try to integrate this in an already existing script?

Please provide more detailed info.

Quote:

plz some expert man help me and check your solution before post
Don't be condescending!

jschiwal 02-25-2011 02:10 AM

There is already a program that does this. `getent'. It looks up entries for various databases such as /etc/services, /etc/hosts.

>getent services 4
echo 4/ddp

You can use it to for example test if domain names are being resolved properly:
>getent hosts elite
192.168.1.106 elite.jesnet elite desktop
>getent hosts www.google.com
getent hosts elite
192.168.1.106 elite.jesnet elite desktop
> getent hosts www.google.com
74.125.229.17 www.l.google.com www.google.com
74.125.229.19 www.l.google.com www.google.com
74.125.229.16 www.l.google.com www.google.com
74.125.229.18 www.l.google.com www.google.com
74.125.229.20 www.l.google.com www.google.com

druuna 02-25-2011 02:18 AM

@jschiwal: Doesn't getent only give a result if the service is actually active and only tcp?

Code:

$ getent services 4
$ getent services 80
http                  80/tcp

$ grep " 4/" /etc/services
#                4/tcp    # Unassigned
#                4/udp    # Unassigned

$ grep " 80/" /etc/services
http            80/tcp    # World Wide Web HTTP
http            80/udp    # World Wide Web HTTP


Dark_Helmet 02-25-2011 02:55 AM

I was in the middle of coding this when I saw:
Quote:

Originally Posted by mmhs
check it man u mistake it's wrong 100% it will return nothing

plz some expert man help me and check your solution before post

I'm not a big fan of complaining about possible solutions when you have not provided anything to start with; even though you say it's a "simple question in bash programming"

So, this code is half-finished, and I intend to leave it that way.

Code:

#!/bin/bash

rawPort=""
while [ -z "${rawPort}" ] ; do
  echo -n "Enter port: "
  read rawPort
done

let validatePort=rawPort+0

if [ "${rawPort}x" = "${validatePort}x" ] ; then
  numericPort=1
else
  numericPort=0
fi

if [ ${numericPort} -eq 1 ] ; then
  lineNumbers=$( sed 's@/@ @g' /etc/services | \
                awk "{if(\$2==\"${validatePort}\") print NR}" )
  for lineNumber in ${lineNumbers} ; do
    sed -n "${lineNumber}p" /etc/services
  done
else
  echo "unsupported"
fi

Like I said: half finished. Does it work 100% of the time? I don't know. Am I going to test it any further? Nope.


All times are GMT -5. The time now is 04:59 PM.