Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Save grep result to a variable

Posted on 2005-03-18
5
Medium Priority
?
3,464 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
  • 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

578 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