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.

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. :wink: I could figure a lot out by googling and reading various tutorials, but not this part. :S

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

./myscript.sh ../somewhere

Does this mean your script expects some arguments ?

in creating a directory scripts in your $HOME and

export PATH=$PATH:$HOME/scripts

suffice?

If not, why?

I enter:

./myscript.sh anypath

...where anypath can be any path, relative or not. Eg:

../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):

/etc/stuff/test
/home/user/foobar
/scripts/files
/scripts/files/extra

...and save that full path into a variable.

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

No, that's exactly what it can't do since the directory specified might not exist.

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.

Actually the first example was using a relative path as well.

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.

What do you think would be the simplest test?
Never thought about using ls?
If it displays: "No such file or directory " then you dont cd...

I'm not looking for a way to find a directory, I'm looking for a way to get the full path based on a shell script's working directory and a directory argument sent to the script, into a variable.

The only option I see is testing the user input to the shell to see what you want to do from there. A case/esac structure might work well in this instance. Perhaps coupled with "dirname"

Something like

 
USERIN=$1 

case "$(dirname $USERIN)" in
  /) echo fully qualified;;
  .) echo relative to current directory;;
  ..) echo relative to parent directory;;
  ../..) echo relative to parent/parent directory;;
...  # < --- replace this
esac

You need to replace the ellipses with one test for each level it can go beyond the script's parent/parent directory back to root.

Obviously you replace the echo statements with code that actually works for you.

dirname seems to know that if you give the name "somewhere" or the name "./somewhere" it will translate that to a single dot "." so that reduces the amount of
tests by one.

And of course after I post that, I realize there's still a host of things that wont work correctly, depending upon input like (../somewhere/else) but maybe this will get you started somewhat? :slight_smile:

Looking more and more like homework, and no collaboration ( you havent shown so far anything of your work without paying attention to what is said there...)
So until you can prove it is not, this thread is considered as homework by someone unwilling to learn and closed

---------- Post updated at 18:06 ---------- Previous update was at 18:05 ----------

Sorry rwuerth...