taking the end off a path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting taking the end off a path
# 1  
Old 06-06-2004
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 '{print $1, $2 ,$3}' but that doesn't help unless i can get it to count from the end of the string.

What should i do?
# 2  
Old 06-06-2004
use basename to get the file name
and dirname to get the path.

Code:
program=/this/is/the/path/to/my/file.ksh

FILE=$(basename program)
DIR=$(dirname program)

echo $FILE
file.ksh

echo $DIR
/this/is/the/path/to/my

Dont use this code since its better to use built-ins rather than
writing something like this, but, here is a method in Awk to
get the parent directory of a file.


echo $PARENT_PATH | awk ' {
     
              arraySize = split($0, parentPath, "/");
              delete parentPath[arraySize];
    
              for (i in parentPath ) {
               w = w "/" parentPath[i];
  
              }
              print w
            }'


Last edited by google; 06-06-2004 at 10:42 AM..
# 3  
Old 06-06-2004
with a few minor changes it works great

thanks a lot :-)
# 4  
Old 06-06-2004
what were the minor changes?
# 5  
Old 06-06-2004
by minor changes i mean adapting it for my script and changing for example "FILE=$(basename program)" to "FILE=$(basename $program)"

I have also encountered another problem on closer investigation.

$0 which provides the input for dirname and basename does not put a \ before spaces which dirname especially doesn't like.

the only way around this that i can think of is to use something like;

no1=$($0 | awk '{print $1}')
no2=$($0 | awk '{print $2}')
no3=$($0 | awk '{print $3}')

execpath=$(echo $no1\ $no2\ $no3)

exedir=$(dirname $execpath)
exefile=$(filename $execpath)

however this would require me to know how many spaces exactly will be in the files path.

This is starting to look complex.
What do i do?
# 6  
Old 06-06-2004
This is where you use quotes most liberally:

exedir=$(dirname "$0")
exefile=$(filename "$0")
# 7  
Old 06-06-2004
Can you post some or all of your script? It would be easier to see the script to better trouble shoot it.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

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

4. Solaris

Test program running taking much more time on high end server T5440 than low end server T5220

Hi all, I have written the following program and run on both T5440 and T5220 on same OS version. I found that T5540 server takes more time than T5220. Please find below the details. test1.cpp #include <iostream> #include <pthread.h> using namespace std; #define NUM_OF_THREADS 20... (17 Replies)
Discussion started by: sanjay_singh85
17 Replies

5. UNIX for Dummies Questions & Answers

Ensuring / at end of directory path

I have a variable name storing a directory path 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... (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

Use of Begin IF ,END IF END not working in the sql script

Hi I have written a script .The script runs properly if i write sql queries .But if i use PLSQL commands of BEGIN if end if , end ,then on running the script the comamds are getting printed on the prompt . Ex :temp.sql After connecting to the databse at the sql prompt i type... (1 Reply)
Discussion started by: isha_1
1 Replies

7. Shell Programming and Scripting

taking a part of string from a given path

Hi I have two path as follows system/console/bin/code/sample/testfile.txt system/console/bin/database/files/new/dbfile.txt I need the output as code/sample in first case database/files/new in second case That is I am omitting system/console/bin and the filename(s) in both... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

8. Shell Programming and Scripting

Need UNIX shell scripting end to end information

Hi, I would like to learn shell scripting in UNIX. Can any one please give me the support and share the information/documents with me. If any documents please post it to aswanikumar_nimmagadda@yahoo.co.in Thanks in advance...!!! (3 Replies)
Discussion started by: aswani_n
3 Replies

9. UNIX for Dummies Questions & Answers

Taking a name out of $PATH

so here is my path: /usr/bin:/bin:/usr/sbin:/sbin:/Users/a:/usr/local/bin:/usr/X11/bin how would I remove only the "a" or name string within it? any help appreciated (7 Replies)
Discussion started by: cleansing_flame
7 Replies
Login or Register to Ask a Question