问题
I have almost no idea about shell scripts or commands in linux
I have a project named projectx
projectX
happens to be in users/hardik/desktop/projectx
I have created a shell script start.sh
. This is the content of shell script
echo "Starting typescript build in new terminal.."
osascript -e 'tell application "Terminal" to do script "npm run build"'
sleep 3
echo "Starting firebase functions...."
osascript -e 'tell application "Terminal" to do script "firebase emulators:start --only functions"'
echo "Process compelete.. Check if there were two terminals window open"
now this works but say here
osascript -e 'tell application "Terminal" to do script "npm run build"'
it runs that in the root and hence gives the following error
ENOENT: no such file or directory, open /Users/hardik/package.json
How can I make it execute in the path which is relative to start.sh
Update: I tried this
echo "Starting typescript build in new terminal.."
path=`pwd`
osascript -e 'tell application "Terminal" to do script "cd ${path} npm run watch:scss"'
osascript -e 'tell application "Terminal" to do script "npm run watch"'
echo "Process compelete.. Check if there were two terminals window open"
but this didn't work with error cd: too many arguments
回答1:
Just change directory to where you need to be prior to running npm
:
osascript -e 'tell application "Terminal" to do script "cd /Users/somewhere && npm run build"'
回答2:
Insert the line:
cd `dirname $0`
at the top of start.sh. The dirname command produces the directory that a file contains, and $0 is the path that invoked start.sh, so that's the relative directory that you wanted. Note those quotes a left-quotes, so the result of running dirname is passed to cd.
来源:https://stackoverflow.com/questions/62658602/shell-script-relative-position-from-the-file