LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script: use the directory of the script file as variable? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-use-the-directory-of-the-script-file-as-variable-782640/)

phling 01-16-2010 03:38 PM

bash script: use the directory of the script file as variable?
 
hi Linux forum.

i'm not actually using Linux but i figured this might be the right place nonetheless...
so i've got this little script file to compile and run some Java code:

Code:

#!/bin/bash
cd /Users/acid/Desktop/javaTest
javac -classpath core.jar Mother.java
java -classpath .:core.jar Mother

is it possible to have it cd to its own location instead, so the whole thing is portable?

thanks

GrapefruiTgirl 01-16-2010 03:46 PM

try:
Code:

cd $(dirname $0)
though this seems sort of redundant.. As it is written, the execution will already be happening in its own location :scratch:

If I'm missing something, please feel free to clarify.

And, welcome to LQ :)
Sasha

schneidz 01-16-2010 03:57 PM

not quite sure what you are looking for but:
pwd will give the present working directory
which <exe-name> will give the path to the executable you are running.

phling 01-16-2010 04:48 PM

okay i'm an utter noob with this shell stuff... didn't even know it executes in its own location :cool:

i still can't get it to work though...
so there is this folder javaTest, in which there's the script executable named "startMother" and both the Mother.java and core.jar files.

so i've tried the script using these:

Code:

cd which<startMother>
cd ..
cd $(dirname $0)

as well as without the cd command, but it seems the directory returned is always "/Users/acid/Desktop/javaTest/startMother" instead of "/Users/acid/Desktop/javaTest"

:scratch:

GrapefruiTgirl 01-16-2010 04:53 PM

This seems a bit round-about :scratch:

Assuming there exists only ONE "javaTest" folder on the machine, you might use:

cd $(find / -type -d -name "javaTest")

but this still seems redundant and weird..

if "which startMother" actually returns the right file, you could use:

cd $(dirname $(which startMother))

but again, weird and redundant. But does this work?

Sasha

phling 01-16-2010 05:05 PM

nope... :cry:

Code:

Last login: Sun Jan 17 00:01:55 on ttys000
/Users/acid/Desktop/javaTest/startMother ; exit;
acid:~ acid$ /Users/acid/Desktop/javaTest/startMother ; exit;
usage: dirname path
javac: file not found: Mother.java
[...etc..]

is bash on Linux and OS X the same thing?

GrapefruiTgirl 01-16-2010 05:11 PM

I don't fully understand the above post.

Let;s start fresh:

Please post:

shell$ ls -la /Users/acid/Desktop/javaTest/

as well as your whole script again (unless it's identical to how it was in post#1) AND, just to refresh the situation again, please explain exactly what it is you're trying to do; the way I understand it is: you want the script to cd to the directory where "startMother" exists, and then execute the rest of the script, yes?

Sasha

phling 01-16-2010 05:35 PM

okay :cool:

Code:

acid:~ acid$ ls -la /Users/acid/Desktop/javaTest/
total 544
drwxr-xr-x  7 acid  staff    238 Jan 17 00:28 .
drwx------@ 39 acid  staff    1326 Jan 16 23:09 ..
-rw-r--r--@  1 acid  staff    6148 Jan 17 00:14 .DS_Store
-rw-r--r--@  1 acid  staff    3389 Jan 16 22:25 Mother.java
-rw-r--r--  1 acid  staff  231565 Oct 20 14:22 core.jar
-rw-r--r--  1 acid  staff  28290 Jan 16 21:35 font.vlw
-rwx------@  1 acid  staff      84 Jan 17 00:17 startMother

yep, the script in post #1 is all and it works fine - as long as the folder javaTest sits neatly on my desktop.

i need the script (which is the file startMother) to work independently from location, so it will run as long as all the above files are in the same folder...

thanks for your patience Sasha

GrapefruiTgirl 01-16-2010 06:06 PM

OK, I'd guess that Bash on OSX is the same, or very nearly identical, to Linux Bash, but this is just a (semi-)educated guess; I've never used OSX so I can't say for sure.

So.. Everything works fine when the folder exists on your desktop; but you want the script to still work if, let's say, the javaTest folder is located somewhere else instead of on your desktop?

The following depends on there being ONLY ONE file "startMother" on the machine.

Code:

#!/bin/bash

MOTHERFILE=$(find / -type f -name "startMother" 2>/dev/null)
MOTHERDIR=$(dirname "$MOTHERFILE")

cd "${MOTHERDIR}"

javac -classpath core.jar Mother.java
java -classpath .:core.jar Mother

So, I tested the above on my machine. I created the Users/acid/... directory path, and made a startMother file that just echoed some crap to the screen. The above script found and executed it, from my home directory/desktop.
I copied the above script to a different location on the machine, and tried it again. It worked. It should also work if the javaTest folder is somewhere else, because this script ONLY looks for the "startMother" file, and cd's to that directory.

I dumped 2>/dev/null because if you are an unpriveleged user, a whole tonne of errors will appear if you do not have permission to search for the file in many locations on a given machine.
And remember: this depends on there being ONLY ONE startMother file on the machine. If there are more than one, weird things will probably happen (which you should error-check for), and also, if no such file is found, the script will fail (which you should also error-check for this occurrence).

So, does this work for you, and do what you want? :)

Sasha

phling 01-16-2010 06:50 PM

Sasha, i appreciate your help a lot.

the above works, however while it was scanning the harddrive i tried around a bit, and found that your very first answer actually does it:
Code:

#!/bin/bash

cd $(dirname $0)
javac -cp core.jar Mother.java
java -cp .:core.jar Mother

and of course much faster than searching the drive for itself :cool:
i'm baffled as to why it didn't work before...

thank you :)

GrapefruiTgirl 01-16-2010 06:52 PM

No problem for the help, you're welcome!

But, looking back at my first answer, I see that the only thing the code in my first answer actually does, is cd to the directory that it's already in, so in effect, doing nothing.

EDIT: It would be the same as:

Code:

cd .
which again, is kinda pointless, because you're already there :)

Have you tried this whole situation WITHOUT the cd line in the script at all?

Cheers!
Sasha

phling 01-16-2010 07:04 PM

yeah both "cd ." and no cd at all result in post #6.
"cd .. " doesn't seem to do anything either. so maybe it's something with OSX? dunno...

anyway it works :cool:

GrapefruiTgirl 01-16-2010 07:16 PM

Interesting... Perhaps it is an OSX-ism..

Anyhow, very happy it works for you now :)

Sasha


All times are GMT -5. The time now is 12:31 PM.