Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
@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.
@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
$ 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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.