ksh - walking back up a directory PATH


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers ksh - walking back up a directory PATH
# 8  
Old 12-03-2008
actually it does sort of work but then goes into a loop, e.g:

$ ./test.ksh
before: Packages/Software/weblogic/9.2mp2/SunOS
after : Packages/Software/weblogic/9.2mp2
before: Packages/Software/weblogic/9.2mp2
after : Packages/Software/weblogic
before: Packages/Software/weblogic
after : Packages/Software
before: Packages/Software
after : Packages
before: Packages
after : Packages
before: Packages
after : Packages
before: Packages
after : Packages
before: Packages
after : Packages
.
.
.
ad infinitum...
# 9  
Old 12-03-2008
just realised why it didn't work, i was supplying the path without a preceeding / , e.g

var="Packages/Software/weblogic/9.2mp2/SunOS"

rather than

var="/Packages/Software/weblogic/9.2mp2/SunOS"

However i don't want to supply a preceeding / in the argument presented to the script, so i'll add that within the script.

Looks like this is the solution to go with. many thanks to everyone who contributed.

cheers

Steve
# 10  
Old 12-03-2008
Hi.

The string expression and utility dirname do essentially the same job, but with slightly different results for the end case. The expression will be faster, but for single-shot use, there is likely to be little contributing difference in time. Here are some some ways to deal with both:
Code:
#!/bin/ksh -

# @(#) s2       Demonstrate comparison of dirname and expression.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset

echo

var="/a/b/c/d/e/f"

echo " Expression of pathname at /:"
while [ -n "$var" ] ; do
     echo - "before: \"$var\""
     var="${var%/*}"
     echo - "after : \"$var\""
done

var="/a/b/c/d/e/f"
echo
echo " dirname of pathname at /:"
while [ "$var" != "/" ] ; do
     echo - "before: \"$var\""
     var="$(dirname $var)"
     echo - "after : \"$var\""
done

var="t/u/v/w/x/y/z"
echo
echo " Expression of pathname, NOT at /:"
while echo "$var" | grep '/' >/dev/null ; do
     echo - "before: \"$var\""
     var="${var%/*}"
     echo - "after : \"$var\""
done

var="t/u/v/w/x/y/z"
echo
echo " dirname of pathname, NOT at /:"
while [ "$var" != "." ] ; do
     echo - "before: \"$var\""
     var="$(dirname $var)"
     echo - "after : \"$var\""
done

exit 0

Producing:
Code:
$ ./s2

(Versions displayed with local utility "version")
SunOS 5.10
ksh M-11/16/88i

 Expression of pathname at /:
- before: "/a/b/c/d/e/f"
- after : "/a/b/c/d/e"
- before: "/a/b/c/d/e"
- after : "/a/b/c/d"
- before: "/a/b/c/d"
- after : "/a/b/c"
- before: "/a/b/c"
- after : "/a/b"
- before: "/a/b"
- after : "/a"
- before: "/a"
- after : ""

 dirname of pathname at /:
- before: "/a/b/c/d/e/f"
- after : "/a/b/c/d/e"
- before: "/a/b/c/d/e"
- after : "/a/b/c/d"
- before: "/a/b/c/d"
- after : "/a/b/c"
- before: "/a/b/c"
- after : "/a/b"
- before: "/a/b"
- after : "/a"
- before: "/a"
- after : "/"

 Expression of pathname, NOT at /:
- before: "t/u/v/w/x/y/z"
- after : "t/u/v/w/x/y"
- before: "t/u/v/w/x/y"
- after : "t/u/v/w/x"
- before: "t/u/v/w/x"
- after : "t/u/v/w"
- before: "t/u/v/w"
- after : "t/u/v"
- before: "t/u/v"
- after : "t/u"
- before: "t/u"
- after : "t"

 dirname of pathname, NOT at /:
- before: "t/u/v/w/x/y/z"
- after : "t/u/v/w/x/y"
- before: "t/u/v/w/x/y"
- after : "t/u/v/w/x"
- before: "t/u/v/w/x"
- after : "t/u/v/w"
- before: "t/u/v/w"
- after : "t/u/v"
- before: "t/u/v"
- after : "t/u"
- before: "t/u"
- after : "t"
- before: "t"
- after : "."

This worked on Linux and Solaris with ksh, pdksh, and bash. See man ksh and man dirname for details ... cheers, drl
# 11  
Old 12-03-2008
Quote:
Originally Posted by surfbus78
just realised why it didn't work, i was supplying the path without a preceeding / , e.g

var="Packages/Software/weblogic/9.2mp2/SunOS"
You could simply change the termination clause of the while loop then, yes? I just used "-n" (which means "nonzero", that is "empty" for strings) but you could use something else:

Code:
while [ "$oldvar" != "$var" ] ; do
     print - "before: $var"
     oldvar="$var"
     var="${var%/*}"
     print - "after: $var"
done

I would prefer using a solution which avoids external programs (like the grep in drls example) as long as this is possible because it slows down the execution of a shell script considerably. You might want to read this enightening thread for an in-depth discussion about the topic.

For the same reason (although this is only remotely connected with the threads theme), btw., i would suggest solely using "print" and not "echo" in a ksh script. "print" is a built-in command in ksh, while "echo" is not - in bash it is the other way round.

I hope this helps.

bakunin

Last edited by bakunin; 12-03-2008 at 11:39 AM..
# 12  
Old 12-03-2008
Hi.

I can't post in the other thread because it's locked, so I'll say a few words here.

There seems always to have been a tension, a trade-off between the choices for optimizing:

1) computer time

2) developer time

3) portability

The genius of Unix was to make it easy to port to different architectures, so that's generally where I start, and often with shell scripts, because they are so easily transportable. Scripting languages generally minimize developer time, an important measure for managers and organizations. As was mentioned in the other thread, if computer time really needs to be minimized, we'd move away from scripting. As we get better, accumulating experience and knowledge, as machines get faster, as software paradigms reveal more about the nature of problems and solutions, we can make choices that may harmonize all three characteristics ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. 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. UNIX for Dummies Questions & Answers

Move back to a directory

~/assign1b/assign1/york/> your current directory is york how do you go back to your home directory with using relative pathname? not using cd alone? :( (3 Replies)
Discussion started by: blackendstars
3 Replies

5. UNIX for Dummies Questions & Answers

alias to go back to previous directory?

Some time ago I was using a linux system and someone had set up and alias called "back" that got me back to the directory I was last in, which was very useful. I've now switched to using a mac, and don't have those aliases available. Can anyone tell me how I could make a an alias that would... (2 Replies)
Discussion started by: ac2011
2 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. Shell Programming and Scripting

Puzzling Problem: Going back to the previous directory

Hi, I am trying to figure out if there is a way to make linux take me back to the previous directory I was working in. For instance, I am in /home/username/directory1 Then if I cd into /home/username/directory1/temp1/temp2/temp3 I would like to immediately go back to the previous... (2 Replies)
Discussion started by: Legend986
2 Replies

8. Linux

how to recover from back up directory

Hey all, Is there any way out to get/restore the deleted file in Linux ? As we do have Recycle Bin in Windows OS. as i deleted some files.. it is available in .snapshot but its a large files.. i cont use "mv" command to copy back. it there any way .. such like in window we click restore... (1 Reply)
Discussion started by: mail2sant
1 Replies

9. UNIX for Dummies Questions & Answers

Path autocompletion in ksh

Say, I want to move into dir library from current dir, on prompt if I type in cd li if it is followed by pressing 'Tab' key then complete dir name appears. Would there anyone know, how we can make into effect this path autocompletion? I am using 'ksh'. (5 Replies)
Discussion started by: videsh77
5 Replies

10. UNIX for Dummies Questions & Answers

? question mark, how to get back to the root directory

hiyas I am trying to get back to the root directory: I went into MAIL directory and now I can't get back to the root directory. What are the commands... I have '?' coming up and I cannot proceed with this, HELP Cheers (1 Reply)
Discussion started by: etravels
1 Replies
Login or Register to Ask a Question