确定执行BASH脚本的路径 [英] Determine the path of the executing BASH script

查看:129
本文介绍了确定执行BASH脚本的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in\">Can一个Bash脚本告诉哪个目录下的存储?

在Windows命令脚本,可以判断当前执行脚本的使用%〜DP0 中的目录的路径。例如:

In a Windows command script, one can determine the directory path of the currently executing script using %~dp0. For example:

@echo Running from %~dp0

什么是在 BASH 脚本等价?

推荐答案

有关的相对路径(即直接等同的Windows%〜DP0 ):

For the relative path (i.e. the direct equivalent of Windows' %~dp0):

MY_PATH="`dirname \"$0\"`"
echo "$MY_PATH"

对于绝对的,标准化的路径:

For the absolute, normalized path:

MY_PATH="`dirname \"$0\"`"              # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`"  # absolutized and normalized
if [ -z "$MY_PATH" ] ; then
  # error; for some reason, the path is not accessible
  # to the script (e.g. permissions re-evaled after suid)
  exit 1  # fail
fi
echo "$MY_PATH"

这篇关于确定执行BASH脚本的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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