Nadaraj
Nadaraj

Reputation: 569

How to take /etc/services entry inside a shell script?

I have an entry in /etc/services file.

abcde 25354/tcp

I need to take the port number of the entry abcde inside my shell script. How to do so?

Upvotes: 0

Views: 1124

Answers (2)

Zan Lynx
Zan Lynx

Reputation: 54325

awk '/^imap / { split($2, a, "/"); print a[1]; }' /etc/services

You will need to replace imap with abcde, or whatever you are really looking for. You might even need to extend the pattern like ^imap .*tcp

You may also like

getent services imap

Which produces:

imap                  143/tcp imap2

Upvotes: 3

falsetru
falsetru

Reputation: 368964

Using grep:

grep ^abcde /etc/services | grep -o '[0-9]*'

The first grep is to get line with abcde, the second is to get digits part only.

Upvotes: 0

Related Questions