Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

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
0
atom_jelly
Asked:
atom_jelly
  • 7
  • 5
  • 3
  • +3
3 Solutions
 
farzanjCommented:
PATH=$(echo $PATH | sed 's/:://g')

Open in new window

0
 
farzanjCommented:
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
 
atom_jellyAuthor Commented:
hello farzanj,

Trying to put that in a script so when it runs can i put this in a find statement?
0
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.

 
farzanjCommented:
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
 
ozoCommented:
PATH=${PATH//::}
0
 
ThomasMcA2Commented:
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
 
farzanjCommented:
@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
 
simon3270Commented:
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
 
atom_jellyAuthor Commented:
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
 
ThomasMcA2Commented:
Duh, ignore my comment. I thought about the ./something issue right after submitting, but didn't try to modify my post in time.
0
 
farzanjCommented:
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
 
farzanjCommented:
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
 
atom_jellyAuthor Commented:
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
 
ozoCommented:
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
 
atom_jellyAuthor Commented:
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
 
atom_jellyAuthor Commented:
I've requested that this question be deleted for the following reason:

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now