Link to home
Start Free TrialLog in
Avatar of atom_jelly
atom_jellyFlag for United States of America

asked on

if statement in linux

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
Avatar of farzanj
farzanj
Flag of Canada image

PATH=$(echo $PATH | sed 's/:://g')

Open in new window

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

Avatar of atom_jelly

ASKER

hello farzanj,

Trying to put that in a script so when it runs can i put this in a find statement?
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 ...
PATH=${PATH//::}
Avatar of ThomasMcA2
ThomasMcA2

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.
@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.
SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Duh, ignore my comment. I thought about the ./something issue right after submitting, but didn't try to modify my post in time.
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
I've requested that this question be deleted for the following reason:

I have resolved this issue.
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
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.
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.
Do you not think ozo's answer was worth anything, Simon?
@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.