Ensuring / at end of directory path


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Ensuring / at end of directory path
# 1  
Old 01-05-2012
Ensuring / at end of directory path

I have a variable name storing a directory path

Code:
set dirpath = /home/chrisd/tatsh/branches/terr0.50/darwin

I want to ensure there is always a "/" at the end, and if there are more than one "/", that I reduce it to just one.

---------- Post updated at 08:16 AM ---------- Previous update was at 08:13 AM ----------

I have decided to change this, so ensuring there is not any "/" at the end. If there is a one at the end I want to remove it.
# 2  
Old 01-05-2012
Try this, will remove any no. / in end

Code:
echo "$dirpath" | sed 's/[\/]*$//g'

# 3  
Old 01-05-2012
Rksiva's solution above is far more sensible, messing with Bash substitutions recently and went a little bit overboard here Smilie
Code:
dirpath=/etc///
echo $dirpath
/etc///
while [ ${dirpath:$(( ${#dirpath} - 1 ))}  == '/' ] ; do dirpath=${dirpath:0:$(( ${#dirpath} - 1 ))} ;done
echo $dirpath
/etc


Last edited by vbe; 01-05-2012 at 09:33 AM.. Reason: corrected code tags
# 4  
Old 01-05-2012
Assuming you're using some (t)csh variant:

Code:
while ( "$dirpath" =~ "*/" ) 
  set dirpath = "$dirpath:h"
end

Consider the following though: Csh Programming Considered Harmful.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. Shell Programming and Scripting

Ensuring certain processes do not show up in process table

i've read somewhere a long time ago that there's a way to hide the workings of a script in sub functions so that when run, it doesn't show up in the process table. is this really possible? for instance, i need to run a command that has a password in it. and when that;s being run, it can be... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

Adding to the end of the path in .profile

Hi there, I have the following in my path: export PATH=/usr/bin:/usr/sbin But I want to change it programmatically(for the purpose of JBoss automatic(ansible) installs) to: export PATH=/usr/bin:/usr/sbin:/usr/jdk/jdk1.8.0_60 and after that I need to insert the JAVA_HOME programmatically,... (7 Replies)
Discussion started by: hvdalsen
7 Replies

5. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

6. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

7. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

8. Shell Programming and Scripting

Ensuring A String Doesn't Begin With Space

In my continuing struggle with my menu for record manipulation, I've been scripting to add records to the 'records' file. For the most part, I've been successful. I am, however, running into a strange problem. while do ... (9 Replies)
Discussion started by: Silentwolf
9 Replies

9. Shell Programming and Scripting

taking the end off a path

I need a script to be able to take a path such as "/foo/bar/thing" a put the "/foo/bar/" bit in one variable and the "thing" bit in another. I figured awk would probably be the best tool for the job but looking at the man page didn't seem to help. The only way i know how to use awk is with... (12 Replies)
Discussion started by: Nat
12 Replies
Login or Register to Ask a Question