Shell脚本在文件中的相对位置 [英] shell script relative position from the file

查看:191
本文介绍了Shell脚本在文件中的相对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎不了解Linux中的Shell脚本或命令

I have almost no idea about shell scripts or commands in linux

我有一个名为projectx

projectX恰好位于users/hardik/desktop/projectx

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

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:没有这样的文件或目录,请打开/Users/hardik/package.json

ENOENT: no such file or directory, open /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

推荐答案

插入该行:

cd `dirname $0`

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

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.

这篇关于Shell脚本在文件中的相对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆