
私はLinuxのシェルスクリプトやコマンドについて全く知りません。
というファイルがありますprojectx
projectX
ただusers/hardik/desktop/projectx
シェルスクリプトを作成しましたstart.sh
。シェルスクリプトの内容は次のとおりです。
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"
今これはうまくいきますが、ここでは次のように言います。
osascript -e 'tell application "Terminal" to do script "npm run build"'
ルートディレクトリで実行されているため、次のエラーが発生します。
ENOENT: そのファイルやディレクトリはありません。開いています。/Users/hardik/package.json
相対パスで実行するにはどうすればよいですか?start.sh
更新:試してみましたがうまくいきませんでした。
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"
上に録音した内容です。
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
以下のコードスニペットも機能しません。
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"
同じエラー
答え1
答え2
これがMacOSで動作するかどうかわかりません。 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"