Reputation:
I read Can a Bash script tell what directory it's stored in?, it shows I can get script directory by DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
.
I find I can use cd
command to change working directory.
This is the content of import.sh. It is in /Users/gqqnbig/SourceCode/Java/PlayerStatisticReader/bin
.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd DIR
java -cp .;jsoup-1.7.3.jar;mysql-connector-java-5.1.29-bin.jar globalVisionEntertainment.nba.Program %1
This is what I get after executing the script.
Macintosh:PlayerStatisticReader gqqnbig$ pwd
/Users/gqqnbig/SourceCode/Java/PlayerStatisticReader
Macintosh:PlayerStatisticReader gqqnbig$ bin/import.sh
: command not found 2:
: No such file or directoryDIR
: command not found 4:
I execute it in the default terminal in Macintosh.
Why is the command not found? How can I make it work?
Upvotes: 3
Views: 6081
Reputation: 695
For what it's worth, I was looking for a solution on how to schedule a command with specific work directory via crontab schedule.
I found a way to do it in one line with env
.
env -C workdir - command args
Upvotes: 0
Reputation: 4231
you need to write
cd "$DIR"
strictly, you only need to add the dollar, but you should also quote the path because it may contain spaces. as to the command not found messages; I don't know. You can remove the empty lines. my guess is it's an encoding issue. do you get the "command not found" output if you paste the script directly into your terminal instead of running the file?
Upvotes: 4