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 thanks |
try:
Code:
cd $(dirname $0) If I'm missing something, please feel free to clarify. And, welcome to LQ :) Sasha |
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 :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> :scratch: |
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 |
nope... :cry:
Code:
Last login: Sun Jan 17 00:01:55 on ttys000 |
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 |
okay :cool:
Code:
acid:~ acid$ ls -la /Users/acid/Desktop/javaTest/ 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 |
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 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 |
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 i'm baffled as to why it didn't work before... thank you :) |
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 . Have you tried this whole situation WITHOUT the cd line in the script at all? Cheers! Sasha |
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: |
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. |