bash script: use the directory of the script file as variable?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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"
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..]
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?
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...
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.