Ensuring / at end of directory path


 
Thread Tools Search this Thread
# 1  
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  
Try this, will remove any no. / in end

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

# 3  
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  
Assuming you're using some (t)csh variant:

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

Consider the following though: Csh Programming Considered Harmful.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. Shell Programming and Scripting

Keep last directory from path

Hello, I am looking for a command that will give me the last directory name from a path ex 1 : /dir1/dir/2/dir3/ output needed dir3 ex 2 : /dir1/dir/2/dir3/dir4/ output needed dir4 (1 Reply)
Discussion started by: Aswex
1 Replies

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

How to get directory name from its path?

If I the path to a directory, what command can I use to return the actual name of that directory. test=`pwd`/folder1 > $test folder1 I'd rather avoid anything with regular expressions. Any ideas? (1 Reply)
Discussion started by: ordano
1 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

Featured Tech Videos