LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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: 676
Blog Entries: 2

Rep: Reputation: 36
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: 12,352

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
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: 147Reputation: 147
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
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
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: 676
Blog Entries: 2

Original Poster
Rep: Reputation: 36
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: 147Reputation: 147
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
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
@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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 09:16 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration