LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-16-2010, 03:38 PM   #1
phling
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 0
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
 
Old 01-16-2010, 03:46 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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

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

And, welcome to LQ
Sasha

Last edited by GrapefruiTgirl; 01-16-2010 at 03:51 PM.
 
Old 01-16-2010, 03:57 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,309

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.
 
Old 01-16-2010, 04:48 PM   #4
phling
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
okay i'm an utter noob with this shell stuff... didn't even know it executes in its own location

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"

 
Old 01-16-2010, 04:53 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
This seems a bit round-about

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
 
Old 01-16-2010, 05:05 PM   #6
phling
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
nope...

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?

Last edited by phling; 01-16-2010 at 05:11 PM.
 
Old 01-16-2010, 05:11 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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
 
Old 01-16-2010, 05:35 PM   #8
phling
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
okay

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

Last edited by phling; 01-16-2010 at 06:00 PM.
 
Old 01-16-2010, 06:06 PM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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

Last edited by GrapefruiTgirl; 01-16-2010 at 06:11 PM. Reason: I added some quotes which really should be there around the $VARIABLES
 
Old 01-16-2010, 06:50 PM   #10
phling
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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
i'm baffled as to why it didn't work before...

thank you
 
Old 01-16-2010, 06:52 PM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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

Last edited by GrapefruiTgirl; 01-16-2010 at 06:55 PM.
 
Old 01-16-2010, 07:04 PM   #12
phling
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 01-16-2010, 07:16 PM   #13
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Interesting... Perhaps it is an OSX-ism..

Anyhow, very happy it works for you now

Sasha
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Script --> read a file & cp -r to my ~/ directory manwithaplan Linux - Newbie 6 10-05-2009 11:25 PM
Bash script to download a directory structure and a file... JoeBleaux Programming 2 03-21-2009 11:21 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
file or directory? bash script efus Programming 3 04-26-2007 06:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration