Solved

if statement in linux

Posted on 2013-01-10
24
271 Views
Last Modified: 2013-04-06
How could I put a if statement that will look at $PATH and remove any consecutive colons

:: in the path of $PATH.

What I am trying to do is remove from $PATH anything listing the current working directory if only it is there, from any global initialization files.  for example from echo $PATH
0
Comment
Question by:atom_jelly
  • 7
  • 5
  • 3
  • +3
24 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38763372
PATH=$(echo $PATH | sed 's/:://g')

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 38763381
The above answers the first part of your question.

The current directory should appear not as :: but should be as :.


In this case it should be like
PATH=$(echo $PATH | sed 's/:\.//g')

Open in new window

0
 

Author Comment

by:atom_jelly
ID: 38763407
hello farzanj,

Trying to put that in a script so when it runs can i put this in a find statement?
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38763428
You mean the entire path in the find command?

You can do like
mypath=$(echo $PATH | sed 's/:\.//g;s/:/ /g')

Open in new window

find $mypath ...
0
 
LVL 84

Expert Comment

by:ozo
ID: 38763551
PATH=${PATH//::}
0
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 38763965
The current directory is automatically searched before the PATH is searched. Therefore, it does not need to be part of the path itself, and IS NOT part of the path on any Linux (or even Windoze) system I have ever used. If it is in your PATH, then it would be better to fix the custom code that modified the PATH variable.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38763986
@ThomasMcA2:
The current directory is automatically searched before the PATH is searched

Not in Linux or Unix.

If that was the case, why do you execute a program like

./a.out
a.out would not work alone.
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 167 total points
ID: 38763989
All of the above will remove the :: from the $PATH, so:
   /usr/bin:/bin::/usr/local/bin
would become
  /usr/bin:/bin/usr/local/bin
which is not what you want.

You need to replace "::" with ":" using either of the above methods:
  PATH=$(echo $PATH | sed 's/::/:/g')
or, if you are using BASH
  PATH=${PATH//::/:}

You'll also need to remove colons from the start and end of PATH. so
  PATH=$(echo $PATH | sed -e 's/::/:/g' -e 's/^://' -e 's/:$//')
or
  PATH=${PATH//::/:}
  PATH=${PATH%:}
  PATH=${PATH#:}
0
 

Author Comment

by:atom_jelly
ID: 38763992
What is this is going to do?

if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)"
      then
             if [  "$2$ = "after"  ]
             then
                    PATH=$PATH:$1
             else
                   PATH=$1:$PATH
             fi
fi
0
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 38764048
Duh, ignore my comment. I thought about the ./something issue right after submitting, but didn't try to modify my post in time.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 31

Expert Comment

by:farzanj
ID: 38764065
Lets say you want to get rid of :: or :.
As simon rightly says you need to change :: to : and if you have :. or :.: you want to get rid of :.

So
PATH=$(echo $PATH | sed 's/::/:/g;s/:\.//g')

Open in new window


You don't need if condition to solve this problem
0
 
LVL 31

Accepted Solution

by:
farzanj earned 167 total points
ID: 38764074
Since you want to use it in the find command you don't need to modify the PATH variable.

You can have something like

mypath=$(echo $PATH | sed 's/::/:/g;s/:\.//g')

Open in new window

0
 

Author Comment

by:atom_jelly
ID: 38764288
I am sorry I need more help with this let me give you guys what my requirements are from the DoD Stig for this:

I need an if statement to address this requirement.

Thanks Good People!

Check Content:
Check the global initialization files' executable search paths.

Procedure:
# grep PATH /etc/bashrc /etc/csh.cshrc /etc/csh.login /etc/csh.logout /etc/environment /etc/ksh.kshrc /etc/profile /etc/suid_profile /etc/profile.d/*

This variable is formatted as a colon-separated list of directories. If there is an empty entry, such as a leading or trailing colon, or two consecutive colons, this is a finding. If an entry begins with a character other than a slash (/) this is a relative path, this is a finding.

Fix Text:
Edit the global initialization file(s) with PATH variables containing relative paths. Edit the file and remove the relative path from the PATH variable.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 166 total points
ID: 38764673
If an entry begins with a character other than a slash (/) this is a relative path
So entries beginning with $ are relative paths

perl -i -pe '/^\s*PATH\b/&&(s#(?<=["=:])[^:/\n"][^:"\n]*##g,s/::+/:/g)' /etc/bashrc /etc/csh.cshrc /etc/csh.login /etc/csh.logout /etc/environment /etc/ksh.kshrc /etc/profile /etc/suid_profile /etc/profile.d/*
0
 

Author Comment

by:atom_jelly
ID: 38766922
Hello Ozo.

I am trying to understand perl myself since I usually script in bash this very useful I just want to be able to explain what its going to do.

perl -i -pe '/^\s*PATH\b/&&(s#(?<=["=:])[^:/\n"][^:"\n]*##g,s/::+/:/g)'

Thanks Ozo.
0
 

Author Comment

by:atom_jelly
ID: 39027147
I've requested that this question be deleted for the following reason:

I have resolved this issue.
0
 
LVL 11

Expert Comment

by:tel2
ID: 39027148
Hi Mods,

I see that atom_jelly is trying to delete this question with the reason "I have resolved this issue.".  I object on the following grounds:
1. Lots of advice has been given in response to atom_jelly's questions.
2. The questions seem to be evolving.
3. Since atom_jelly has not stated what solution (s)he used, we have no evidence that no part of the solution was derived from any of the answers given by experts.

Enjoy.

Thanks.
tel2
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39027278
I second tel2.  A lot of very valid and working solutions were given in response to this question and atom_jelly could have used a number of solutions to solve his problem.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39030629
I'm with tel2 and farzanj - we answered the question (once we'd sorted out that "::" needed to go to ":"), and the only things outstanding were analysis of the OP's shell fragment (which was adding paths, not removing them) and a decription of ozo's Perl, but both of these were well outside the remit of the original question.

I'd suggest splitting the points between my comment http:#a38763989 which first does :: to :, , and farzanj's http:#a38764074 which adds removing the . on its own.
0
 
LVL 11

Expert Comment

by:tel2
ID: 39036066
Do you not think ozo's answer was worth anything, Simon?
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39038094
@tels2, ozo's probably is worth adding to the mix - he was answering after the "how do I add paths" question from the OP, and did get thrown by the "paths starting with $ are relative (which they aren't necessarily), but he did give some code to manipulate PATHs in the input files.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now