0

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 these but it didn't work

cd "$(dirname $(readlink -f "$0"))"
echo "Starting typescript build in new terminal.."
osascript -e 'tell application "Terminal" to do script "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"

This is what it logs for the above

readlink: illegal option -- f
usage: readlink [-n] [file ...]
usage: dirname path
Starting typescript build in new terminal..
tab 1 of window id 1579
tab 1 of window id 1580

also the below snippet doesn't work either

cd -P -- "${0%/*}" || exit
echo "Starting typescript build in new terminal.."
osascript -e 'tell application "Terminal" to do script "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"

Same error

3
  • Why do you need to npm build in a new Terminal window? Jun 30, 2020 at 15:48
  • @glennjackman npm watch. I want one command to start everything. Since both of them are watch, they might also throw error if something goes wrong. for that, I don't want things to happen in the same terminal and I also don't want to run two npm commands whenever I start a project. Jun 30, 2020 at 21:34
  • Possibly helpful (but I can't say whether it works on macOS): unix.stackexchange.com/a/50392/315749
    – fra-san
    Jul 1, 2020 at 22:08

2 Answers 2

1

You can add cd -P -- "${0%/*}" || exit or cd -P -- "$(dirname -- "$0")" || exit to your script to jump into the directory in which the script resides.

$0 is a built-in variable holding the path of the script as given to the interpreter (will typically be an absolute path if the script was looked up in $PATH).

The ${0%/*} construct uses the shell's built-in String Manipulation to remove from the end the shortest bit of the variable matching /*, which leaves you with just the directory name (would work as long as $0 contains at least one / character which is generally the case).

5
  • 1
    $0 doesn't always contain the full name of the script, it generally contains the name used to invoke the script. Jun 30, 2020 at 14:58
  • Right, but if it's in your path, $0 is set to the full path. Otherwise you specify either the absolute or relative path, and cd will still succeed in putting you into that absolute or relative path.
    – user420417
    Jun 30, 2020 at 15:03
  • But nothing in the OP suggests that his script is in his path. Jun 30, 2020 at 15:23
  • @user420417 I tried what you said based on my underdstanding. Didn't work (updated question for the same) Jun 30, 2020 at 21:36
  • You may use something as if [ "$0" = "${0#/*}" ]; then mypath=${PWD}/${0%/*}; else mypath=${0%/*}; fi to also deal with the sh script kind of invocation (that has no slash in "$0"). And (though I don't know whether it's supported on macOS) readlink -f "$0" to handle the case of a script invoked as a symbolic link.
    – fra-san
    Jul 1, 2020 at 22:16
0

I do not know whether this works under MacOS; it does under Linux:

#! /bin/bash
  
arg1="$(awk 'BEGIN { RS="\0" }; NR==2 { print $0 "x"; exit; }' /proc/$$/cmdline; echo x)"
arg1="${arg1%x}"

if [[ $arg1 =~ / ]]; then
    # path contains at least one /
    dir_rel_path="${arg1%/*}"
    cd "$dir_rel_path"
fi

echo "$PWD"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.