Custom directory path variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Custom directory path variable
# 1  
Old 10-08-2010
Custom directory path variable

I'm trying to write my first shell script and got a bit stuck with this:

I've got myscript.sh that executes from /fromhere.

If the script is run with the syntax ./myscript.sh tothere:
I need to make a variable inside the script containing /fromhere/tothere

...and if the script is run with the syntax ./myscript.sh ../somewhere:
I need that variable to contain /somewhere

Is there some way to do this without manually making if statements for entires like "", ".", "..", "/whatever", "whatever/whenever/however" by doing extensive substring'ing or similar to the variables?


EDIT:
I can't cd into the category and use $PWD or $0 either since the category specified might be one that does not currently exist.
# 2  
Old 10-08-2010
Homework?
# 3  
Old 10-08-2010
Quote:
Originally Posted by vbe
Homework?
Kids have homework in shell scripting now? O.o

Nah, I wanted to make it quicker to run an application for which i often use similar syntaxes and figured it would be nice to learn some shell scripting since I've got my own linux server I've been playing around with. Smilie I could figure a lot out by googling and reading various tutorials, but not this part. :S
# 4  
Old 10-08-2010
OK...
I suppose the fact that you had no replies has something to do with the way you presented your issue, I, for example (but I dont truly speak english...) try to imagine what you are talking about and finish up not understanding...

It would help if we knew what your script is about.. e.g
Code:
./myscript.sh ../somewhere

Does this mean your script expects some arguments ?

in creating a directory scripts in your $HOME and
Code:
export PATH=$PATH:$HOME/scripts

suffice?

If not, why?
# 5  
Old 10-08-2010
I enter:
Code:
./myscript.sh anypath

...where anypath can be any path, relative or not. Eg:
Code:
../etc/stuff/test
/home/user/foobar
files
files/extra

I then want to convert this into the full path, eg (assuming the script is run from /scripts):
Code:
/etc/stuff/test
/home/user/foobar
/scripts/files
/scripts/files/extra

...and save that full path into a variable.
# 6  
Old 10-08-2010
From what I understand now, is :
Your script does a cd to what is given as argument the the command pwd...
the 2 first examples will work not the next ones, why?
Will that be your new assignment?
(clue: Commonly in unix, not giving the path is uderstood as *relative" to current, as you can see why your two last examples will not work. You would now have to write a few tests in your script to see if argument could be a directory in a tree using the command find and hope it is unique if not... I let you choose the way you want to end...

Last edited by vbe; 10-08-2010 at 10:35 AM..
# 7  
Old 10-08-2010
Quote:
Originally Posted by vbe
From what I understand now, is :
Your script does a cd
No, that's exactly what it can't do since the directory specified might not exist.

Quote:
the 2 first examples will work not the next ones, why?
None of the examples work atm since I'm not sure how to do this. I probably could make them all work by making loads of if arguments and substr's. That feels more like a workaround to me though, and I'm looking for a direct way to do this.

Quote:
(clue: Commonly in unix, not giving the path is uderstood as *relative" to current, as you can see why your two last examples will not work.
Actually the first example was using a relative path as well.

Quote:
You would now have to write a few tests in your script to see if argument could be a directory in a tree using the command find and hope it is unique if not... I let you choose the way you want to end...
Like I said, the directory might not exist and the point is to grab directory names from the syntax argument so I wouldn't know what to search for.
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. UNIX for Advanced & Expert Users

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (3 Replies)
Discussion started by: kiran425
3 Replies

5. UNIX for Dummies Questions & Answers

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (2 Replies)
Discussion started by: kiran425
2 Replies

6. UNIX for Dummies Questions & Answers

Make install in custom path

I would like to install a binary from source on a custom path, say /usr/local/myapps. There is no --prefix option in ./configure How can I "make install" at custom path. I tried this. No --prefix root@server # ./configure --help | grep prefix root@server # Make install ... (3 Replies)
Discussion started by: anil510
3 Replies

7. UNIX for Dummies Questions & Answers

Find command fails when a space is in the directory path variable

I have a script like this running under OS X 10.8. The problem arises when the find command encounters a space in the path name. I need the "dir" variable as I'll be extending the script to more general use. #!/bin/bash CFS=$IFS IFS=$(echo) set dir = "/Users/apta/Library/Mail\... (3 Replies)
Discussion started by: apta
3 Replies

8. Shell Programming and Scripting

zlib.h in custom path

I am trying to configure gpac. I get the error as follows. # ./configure error: zlib not found on system or in local libs I have installed zlib on custom path /usr/local/myapps. I know the above error occurs when devel package or .h file is not present. Its present on the server. # ll... (7 Replies)
Discussion started by: anilcliff
7 Replies

9. Shell Programming and Scripting

Variable directory name in path

I need to write a login script for multiple computers, however, one of the directories in question will have a different name from computer to computer. ~/Library/Application\ Support/Firefox/Profiles/<unique filename>.default/myfile For the directory named <unique filename>.default , I... (2 Replies)
Discussion started by: glev2005
2 Replies

10. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies
Login or Register to Ask a Question