LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-22-2011, 10:56 PM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
Grep output help needed


According to linux hardening guide a PATH variable must not contain . or ..
so i grep the path variable using.

Code:
# echo $PATH | grep "."
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# echo $PATH | grep ".."
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
There are no . or .. in the PATH but still the output is produced.

I m little confused How its possible?
Code:
# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# pwd
/usr/local/sbin
What logic can be applied to check the existence of . and .. in PATH variable?
 
Old 05-22-2011, 11:01 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
In that (simple) regex the dot means "any single character" - escape it with a backslash "\."
 
Old 05-22-2011, 11:44 PM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by syg00 View Post
In that (simple) regex the dot means "any single character" - escape it with a backslash "\."
Exactly. You could also enclose it in a character list where it loses its special meaning.

Code:
df_linux$ echo $PATH | grep '\.'
df_linux$ echo $PATH | grep '[.]'
df_linux$

HTH
 
1 members found this post helpful.
Old 05-22-2011, 11:57 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Not sure how exact you might want to be, but you could get a little more specific as whilst the above do work they will also capture something odd
like the following:
Code:
# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/tada.bin
Obviously this is pretty out of the ordinary, but in case you are interested:
Code:
echo $PATH | egrep ':[.]{1,2}[^:]*'
This also covers other weird ones that might be put in your PATH to cause havoc, like:
Code:
../../../../bad_news_bin
 
Old 05-23-2011, 12:13 AM   #5
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Hi that was helpful but now i m stuck at different problem.

Code:
# echo $PATH | grep '[..]'
/usr/kerberos/sbin::/usr/kerberos/bin:.:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Any Idea would be very helpfull.

I have twicked Path variable for script testing.
Code:
# echo $PATH
/usr/kerberos/sbin::/usr/kerberos/bin:.:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Its still showing it has .. in it.
 
Old 05-23-2011, 12:26 AM   #6
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by pinga123 View Post
Hi that was helpful but now i m stuck at different problem.

Code:
# echo $PATH | grep '[..]'
/usr/kerberos/sbin::/usr/kerberos/bin:.:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Because that's not how a character list works. Each character list represents only one of the listed characters. Read the pages I linked for more information about character lists.

Try it like this:

Code:
echo $PATH | grep '[.][.]'
That will specifically only catch instances of two dots (.) in sequence.

Otherwise, just backslash escape the dots:

Code:
echo $PATH | grep '\.\.'
 
Old 05-23-2011, 12:34 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, ideally you don't want '::' or '.' or '..' in your path. The first is merely redundant, not a security issue AFAIK.
You can match a given num eg 2 of a char thus
Code:
t1="ffgg..bnm"
echo $t1 |grep '[.]\{2\}'
see here for some good examples http://www.robelle.com/smugbook/regexpr.html
 
Old 05-23-2011, 08:30 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
@pinga123

You should learn how regular expressions work.

http://www.google.com/search?q=regul...=hp&channel=np
 
0 members found this post helpful.
Old 05-23-2011, 12:10 PM   #9
16pide
Member
 
Registered: Jan 2010
Posts: 418

Rep: Reputation: 83
for fun, try this:
Code:
man man
or use vi to edit any text document
then type:

/.
see that everything is selected

/\.
now only the "." characters are selected

The magic of regular expressions...
 
1 members found this post helpful.
  


Reply

Tags
grep, regular expression



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using grep to output non-matches into output file binny959 Linux - General 5 12-24-2010 11:20 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
ps -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-30-2009 11:21 PM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 10:27 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration