Assigning the relative directory name to a variable called $DIR.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Assigning the relative directory name to a variable called $DIR.
# 1  
Old 09-04-2010
Question Assigning the relative directory name to a variable called $DIR.

Hi All,

I've been trying to write a KSH script that acts on the name of the relative directory that this script is called from.

In other words, if I am executing this script from a directory called:
/hello/hithere/directory

How would I assign
DIR=

to be "directory" and not "/hello/hithere/directory" ?

Thank you so much !
cg
# 2  
Old 09-04-2010
Assuming that the full script name is in $spath (e.g. /usr2/bin/script), then the following code in Kshell will yield the last part of the directory:

Code:
fullpath=${spath%/*}     # full path of directory containing the command (removes script name)
dname=${fullpath##*/}  # strip all things from start to last / in the path

The result, given /usr2/bin/script, would be bin assigned to dname.

Last edited by agama; 09-04-2010 at 01:43 PM.. Reason: fixed typo; clarification in comment
This User Gave Thanks to agama For This Post:
# 3  
Old 09-04-2010
MySQL

Quote:
Originally Posted by agama
Assuming that the full script name is in $spath (e.g. /usr2/bin/script), then the following code in Kshell will yield the last part of the directory:

Code:
fullpath=${spath%/*}     # full path of directory containing the command (removes script name)
dname=${fullpath##*/}  # strip all things from start to last / in the path

The result, given /usr2/bin/script, would be bin assigned to dname.
Perfect! Thank you!
In the case of Solaris, I was able to use the logic above to do it with:
Code:
DIR=${PWD##*/}

Thanks again!
cg
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Show only the filenames under a directory without relative and absolute paths.

I am able to list all the filenames under a directory & its sub-directories except blent.tar on Linux find "/tmp/" -type f | grep -v blent.tar | rev | cut -d '/' -f1 | rev Desired Output: THIRDPARTYLICENSEREADME.txt javaws libjavaplugin_oji.so libjavaplugin_oji.so... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

3. Shell Programming and Scripting

Can a shell variable be called in a cobol program

Hi All, I have a file which sets all the variables on unix , based on the hostname. Currently these variables are hardcoded in the cobol programs.I was wondering if unix variables can be used in Cobol programs ? Example : I have a variable $SHTEMP which is set based on the following : Prod... (2 Replies)
Discussion started by: nua7
2 Replies

4. UNIX for Dummies Questions & Answers

deleting all the files inside a directory from a relative path

I have a file inside abc/def/ghi directory. let say a.txt I need to delete this a.txt from abc itself. I have tried ls /abc/def/ghi | xargs rm -r its saying rm: a.txt non-existent also tried rm -rf /def/ghi but in vein. plz help :) (2 Replies)
Discussion started by: gotam
2 Replies

5. Shell Programming and Scripting

environment variable in shell script called through crontab

Please help me on below.. https://www.unix.com/shell-programming-scripting/141533-retrieve-value-environment-variable-shell-script-called-crontab.html#post302442024 I'm still here. I can still see you! (0 Replies)
Discussion started by: jadoo_c2
0 Replies

6. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

7. Shell Programming and Scripting

How do I define a particular dir in PATH variable and then unset that dir

How do I define a particular dir in front of PATH variable and then run some job and then at the end of job SET the PATH variable to original? in a script, WILL something like this work: ORIG_PATH=$PATH export PATH=/dir1/dir2:$PATH RUN SOME JOBS ..... unset PATH EXPORT... (2 Replies)
Discussion started by: Hangman2
2 Replies

8. UNIX for Dummies Questions & Answers

get cygpath to leave relative paths as relative?

If I execute mypath=`cygpath -w ../` echo $mypath I get d:\unix\nextVersion\script OK, d:\unix\nextVersion\script is the correct windows version of the path, but it is in absolute form. I would prefer it if cygpath left it in relative form, i.e. echo $mypath should output ..\ ... (0 Replies)
Discussion started by: fabulous2
0 Replies

9. UNIX for Dummies Questions & Answers

Help I've created a dir called /

OK, I had some odd characters in my mkdir command which resulted in the following directory getting created: drwxr-xr-x 2 fred fredgroup 512 Apr 12 16:52 / Any ideas how I get rid of it safely? Interestingly that's a straight cut'n'paste and there appear to be some odd... (4 Replies)
Discussion started by: pondlife
4 Replies

10. Shell Programming and Scripting

Relative directory - NOT absolute

Here is the drill, I am using a script to login to a remote ftp, and put and get files. My question is: I want to login and automatically change to the same directory I am in on my machine. I can not use $home, pwd or anyother env variable (that I know) since the names of the machines are totally... (4 Replies)
Discussion started by: sierra_aar
4 Replies
Login or Register to Ask a Question