【问题标题】:shell script relative position from the file文件中的shell脚本相对位置
【发布时间】:2020-10-20 19:47:58
【问题描述】:

我对 linux 中的 shell 脚本或命令几乎一无所知

我有一个名为projectx的项目

projectX恰好在users/hardik/desktop/projectx

我创建了一个 shell 脚本start.sh。这是shell脚本的内容

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的路径中执行

更新:我试过了

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"

但这不适用于错误cd: too many arguments

【问题讨论】:

    标签: macos unix command-line command-line-interface


    【解决方案1】:

    在运行npm之前将目录更改为您需要的位置:

    osascript -e 'tell application "Terminal" to do script "cd /Users/somewhere && npm run build"'
    

    【讨论】:

    • 不同用户克隆 repo 的路径可能不同。他们都会在mac上运行它,这是肯定的。我也更新了问题
    • 您似乎错过了cd somewherenpm run build 之间的&&
    【解决方案2】:

    插入行:

    cd `dirname $0`
    

    在 start.sh 的顶部。 dirname 命令生成文件包含的目录,$0 是调用 start.sh 的路径,因此这是您想要的相对目录。注意那些引号是左引号,所以运行 dirname 的结果会传递给 cd。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多