?
Solved

Save grep result to a variable

Posted on 2005-03-18
5
Medium Priority
?
3,446 Views
Last Modified: 2012-08-13
In a bash script I am trying to save the result of

grep -c "mtftp[[:blank:]]*1759/tcp[[:blank:]]*[#]*" /etc/services

So far, I have tried

result="$?" and result=$?
witch
echo $result

but nothing worked


Thanks for your help

0
Comment
Question by:ladwein
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13573007
Bash places the exit code into $?, so the following will capture the exit code of the GREP:

result=$?

e.g.
$ touch test.txt
$ grep "hello world" test.txt
$ echo $?
1
$ result=$?
$ echo $result
0
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 500 total points
ID: 13573091
did you mean

result=`grep -c "mtftp[[:blank:]]*1759/tcp[[:blank:]]*[#]*" /etc/services`
echo $result

the quotes are backticks `` and not single quotes ' '
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 500 total points
ID: 13573108
ah yes, the output, that could be it..

btw, using $() rather than back ticks can sometimes be more readable, same meaning though...

result=$(grep -c "mtftp[[:blank:]]*1759/tcp[[:blank:]]*[#]*" /etc/services)

And useful to know both, as then they can be combined for "real" power....

Cheers,
C.


0
 
LVL 1

Author Comment

by:ladwein
ID: 13573190
Ah. That is somewhat confusing in my Linux book since it does not tell the difference between the result code (here: 0) and the output grep generates. So it was sunnycoder's answer I was looking for.  I will give 125 Points to both of you since cjjclifford would have known the answer as well.
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13573548
:-)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question