Help with directory path parsing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with directory path parsing
# 1  
Old 02-15-2010
Help with directory path parsing

I've been working on a UNIX script (csh) that will be starting a java application

The goal is to get the version number and location (needed by the application) from the path of the script

Example:
Location of the script= /apps/myapp/versionNum/script/start.csh

I need:
approot=/apps/myapp (as absolute path... no links)
appver=versionNum

I've done a lot of searching and I can't seem to cobble together a solution. Please Help!
# 2  
Old 02-15-2010
Quote:
Originally Posted by brianjbrady
I've been working on a UNIX script (csh) that will be starting a java application

Do not use csh for scripting:

Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful
Quote:

The goal is to get the version number and location (needed by the application) from the path of the script

Example:
Location of the script= /apps/myapp/versionNum/script/start.csh

I need:
approot=/apps/myapp (as absolute path... no links)
appver=versionNum

I've done a lot of searching and I can't seem to cobble together a solution. Please Help!

It's easy in a real shell:
Code:
script=/apps/myapp/versionNum/script/start.csh
IFS=/ read -r x a b c d e <<.
$script
.
approot=/$a/$b
appver=$c

# 3  
Old 02-15-2010
Which 'real' shell do you recommend?
csh does seem like a real pain in the a..
However, its what I was given to work with.
# 4  
Old 02-15-2010
To say that csh is not a real shell is a slight overstatement - after all, it is a "shell".

To say that it's a pain, however, is a complete understatement!

This might influence you: most useful shell
# 5  
Old 02-15-2010
The reality of the situation is that I dont care what shell I use. I just want to get the info I need to run my app.

The previous solution will not work in a situation where there is an indeterminate number of parent directories for myapp.
Code:
script=/apps/myapp/versionNum/script/start.csh
IFS=/ read -r x a b c d e <<.
$script
.
approot=/$a/$b
appver=$c

So the bat script that I wrote on the windows side:
A)looks at the current path goes up one directory
B)stores that directories name in a variable
C)goes up another directory
D)stores that path in a variable

Any other ideas?
# 6  
Old 02-15-2010
Code:
$ cat Test
#!/bin/ksh
S=$(echo $PWD/${0##*/})
V=$(echo $S | awk -F/ '{print $(NF-2)}')
P=${S%%/$V*}

echo Script $S
echo Version $V
echo Path preceding version $P

$ ./Test
Script /apps/myapp/versionNum/script/Test
Version versionNum
Path preceding version /apps/myapp


Last edited by Scott; 02-15-2010 at 06:43 PM..
# 7  
Old 02-16-2010
Thanks!

Do you have any recommendations on good places to learn more about UNIX scripting?
I can kind of follow what you are doing but I dont know what some of the syntax is doing.

At some point I would like to be able to transpose scripts between ksh and bash.

Again thanks for your help.
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. Shell Programming and Scripting

Variable of Path directory is not parsing in awk

Hi All, i had to split one files into 10 equally. For that i have coded below awk. OUTPUT_FILE=/home/sit/path/Files/file_EXPORT.lst DIR_NM=`dirname ${OUTPUT_FILE}` awk -v CURR_DATE="$(date +'%d-%m-%Y-%H-%M')" -v pth=$DIR_NM '{print >> pth/"tgt_file_name"CURR_DATE"_"NR%10 }' ${OUTPUT_FILE} ... (7 Replies)
Discussion started by: looney
7 Replies

4. Shell Programming and Scripting

Parsing a PATH statement

I have a script that will be placing a trigger file for other applications. The user-inputted path is similar to: "/data/region/NorthAm/Project HAV 8H" The project path will not change throughout the script. However, pwd changes as the scanning continues in the script. I need to truncate... (3 Replies)
Discussion started by: leepet
3 Replies

5. Shell Programming and Scripting

Bash script not parsing file with spaces in path

Hi everyone, I'm trying to write my first ever shell script, the OS is Raspbian. The code I have written must be executed whenever a certain database has been modified. The database resides on a Windows server to which I have a mount point, and I have no control over the Windows server at all so... (2 Replies)
Discussion started by: gjws
2 Replies

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

7. Shell Programming and Scripting

Specific directory parsing in a directory tree

Hi friends, Hello again :) i got stuck in problem. Is there any way to get a special directory from directory tree? Here is my problm.." Suppose i have one fix directory structure "/abc/xyz/pqr/"(this will be fix).Under this directory structure i have some other directory and... (6 Replies)
Discussion started by: harpal singh
6 Replies

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

9. Shell Programming and Scripting

parsing path

Can somebody show me a sed or awk command that can parse a pathname My input will look like this /u01/app/oracle/diag/rdbms/ifddb1/ifddb1 I would like my output to get rid of everything including the "/" before diag. So my output will be this diag/rdbms/ifddb1/ifddb1 I need... (1 Reply)
Discussion started by: BeefStu
1 Replies

10. 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
Login or Register to Ask a Question