Function to remove the directories from PATH variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function to remove the directories from PATH variable
# 1  
Old 05-30-2013
Function to remove the directories from PATH variable

Hello,

From the URL

https://www.unix.com/shell-programmin...ariable-2.html

I got a function to remove the directories from a path. but looks like this isnt quite working.. i am also not able to post the comments in the thread as it is closed.

So can anyone help me with this...


Code:
rmpath() #@ remove directory or directories from $PATH {        #@ USAGE: rmpath dir ...     for p in "$@"     do       p=${p%"${p##*[!/]}"} ## remove trailing slashes       case $PATH in           "$p":*) PATH=${PATH#$p:} ;; ## at beginning of PATH           *:"$p") PATH=${PATH%:$p} ;; ## at end of PATH           *:"$p":*) PATH=${PATH%":$p"*}${PATH##*"$p"} ;;       esac     done     export PATH }

Code:


Last edited by satishkumar432; 05-30-2013 at 11:44 AM..
# 2  
Old 05-30-2013
I've done it yesterdy in a ksh script using awk :

Code:
awk -F"/" '{ print $NF} ' < file

file contain the absolute path

basename command can be used too
# 3  
Old 05-30-2013
What's not working? Post input, desired output, error messages or other indicators so we can help!
# 4  
Old 05-30-2013
I am currently the below script in the link. I would like to have a functionality where the directories get
deleted from the $PATH variable of the system.

For Example:

$ echo $PATH

/users/SINLE/:/bin:/usr/bin:/usr/local/bin:/users/smandodd/bin:/sbin


This is the current content of $PATH.

Now i would like to pass the "/usr/bin" as an argument to the function, which
will delete that path from the $PATH variable.some thing like

$./removepath.sh /usr/bin

$echo $PATH

$
/users/SINLE/:/bin:/usr/local/bin:/users/smandodd/bin:/sbin

This functionality is given by the below function.. but for some reason this
isnt working.. ...


https://www.unix.com/shell-programmin...ariable-2.html









rmpath() #@ remove directory or directories from $PATH { #@ USAGE: rmpath dir ... for p in "$@" do p=${p%"${p##*[!/]}"} ## remove trailing slashes case $PATH in "$p":*) PATH=${PATH#$p:} ;; ## at beginning of PATH *:"$p") PATH=${PATH%:$p} ;; ## at end of PATH *:"$p":*) PATH=${PATH%":$p"*}${PATH##*"$p"} ;; esac done export PATH }

---------- Post updated at 09:55 AM ---------- Previous update was at 09:54 AM ----------

[QUOTE=satishkumar432;302814835]I am currently the below script in the link. I would like to have a functionality where the directories get
deleted from the $PATH variable of the system.

For Example:

$ echo $PATH

/users/SINLE/:/bin:/usr/bin:/usr/local/bin:/users/smandodd/bin:/sbin


This is the current content of $PATH.

Now i would like to pass the "/usr/bin" as an argument to the function, which
will delete that path from the $PATH variable.some thing like

$./removepath.sh /usr/bin

$echo $PATH

$
/users/SINLE/:/bin:/usr/local/bin:/users/smandodd/bin:/sbin

This functionality is given by the below function.. but for some reason this
isnt working.. ...


HTML Code:
 https://www.unix.com/shell-programming-scripting/121303-remove-path-path-environment-variable-2.html

Code:
rmpath() #@ remove directory or directories from $PATH {        #@ USAGE: rmpath dir ...     for p in "$@"
 do       p=${p%"${p##*[!/]}"} ## remove trailing slashes       case $PATH in           "$p":*) PATH=${PATH#$p:} ;; ## at beginning of PATH           *:"$p") PATH=${PATH%:$p} ;; ## at end of PATH           *:"$p":*) PATH=${PATH%":$p"*}${PATH##*"$p"} ;;
      esac     done     export PATH


Last edited by satishkumar432; 05-30-2013 at 11:58 AM.. Reason: code tagging
# 5  
Old 05-30-2013
Try
Code:
PATH=$(echo $PATH | sed "s#^$1:##;s#:$1:#:#;s#:$1\$##")

This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-01-2013
Thank YOu..

Quote:
Originally Posted by RudiC
Try
Code:
PATH=$(echo $PATH | sed "s#^$1:##;s#:$1:#:#;s#:$1\$##")


Thank You..Rudic..

For a timely response...this works as expected... thank you very much.. as a novice to awk and sed i was not able to figure out .... A request.. could you explain.how you explored the sed options..if possible.. Thanks for your time.. Smilie
# 7  
Old 06-02-2013
Not sure what you mean by "explore the sed options". If you asked to explain: the sed script consists of three s(ubstitute) commands:
Code:
s#^$1:##        -- subst the parameter $1 incl. ":" at start of line with nothing
s#:$1:#:#       -- subst $1 embedded in ":" in the middle with just one ":"
s#:$1\$##       -- subst ":" + $1 at EOL with nothing

sed, in regexes, takes "^" for begin of line, and "$" for end of line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Adding Directories to PATH in HP-UX with csh

HI Guys, I am truing to add some directories to the productive HP-UX (like /usr/sbin) the problem is that i tried some methods like (export) and setenv but i failed. I add some logs output of the commands to provide you with more info . the user which I am trying to add this path is different... (5 Replies)
Discussion started by: barry1946
5 Replies

2. Shell Programming and Scripting

Print path files in different directories

Hi guys :) First of all Happy New Year :) so i dont know if my doubt its already here posted by other person ... i need to print to one file the path of few files that are in different directories, like this: directory muscle ATP6.aa.muscle.fasta COX1.aa.muscle.fasta . . . ... (2 Replies)
Discussion started by: andreia
2 Replies

3. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

4. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

5. Shell Programming and Scripting

Appending a path in user's PATH variable

Hello Folks, I want to append a path in user's PATH variable which should be available in current session. Background Numerous persons will run a utility. Aim is to add the absolute path of the utility the first time it runs so that next runs have the PATH in env & users can directly run... (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

6. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

7. Shell Programming and Scripting

remove a path from PATH environment variable

Hi I need a script which will remove a path from PATH environment variable. For example $echo PATH /usr/local/bin:/usr/bin:test/rmve:/usr/games $echo rmv test/rmve Here I need a shell script which will remove rmv path (test/rmve) from PATH... (9 Replies)
Discussion started by: madhu84
9 Replies

8. Shell Programming and Scripting

find in given path do not want to traverse to its sub-directories

Hi All, My UNIX Version is: OS Name Release Version AIX appma538 3 5 I want to find certain files with some criterias under the given path. At the same time i want to find the files which resides under the given directory, but normal find traverse to its sub-directories... (4 Replies)
Discussion started by: Arunprasad
4 Replies

9. UNIX for Dummies Questions & Answers

Want to create 3 different new directories under the same path

Hi, Iam new to UNIX...My requirement is to create 3 dir as an hierarchy under /var/opt/temip.The output should be /var/opt/temip/GP_Int/GPTTS/AUTO. I have tried the following script...But only GP_int folder is getting created and not other folders...Can someone help??? #!/usr/bin/ksh #script... (1 Reply)
Discussion started by: Llb
1 Replies

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question