Ok so I have written a .sh file in Linux Ubuntu and it works perfectly. However on a Mac it always returns that the file was not found even when it is in the same directory. Can anyone help me out?
.sh file:
if [ ! -f file-3*.jar ]; then
echo "[INFO] jar could not be found."
exit
fi
Just thought I’d add, this isn’t for more than one file, it’s for a file that is renamed to multiple endings.
In a comment to @Paul R’s answer, you said “The shell script is also in the same directory as the jar file. So they can just double click it after assigning SH files to open with terminal by default.” I suspect that’s the problem — when you run a shell script by double-clicking it, it runs with the working directory set to the user’s home directory, not the directory where the script’s located. You can work around this by having the script cd to the directory it’s in:
EDIT: $BASH_SOURCE is, of course, a bash extension not available in other shells. If your script can’t count on running in bash, use this instead:
BTW, the construct
[ ! -f file-3*.jar ]makes me nervous, since it’ll fail bizarrely if there’s ever more than one matching file. (I know, that’s not supposed to happen; but things that aren’t supposed to happen have any annoying tendency to happen anyway.) I’d use this instead:Again, if you can’t count on bash extensions, here’s an alternative that should work in any POSIX shell:
Note that this will fail (i.e. act as though the file didn’t exist) if there’s more than one match.