检索脚本的父目录 [英] Retrieve parent directory of script

查看:47
本文介绍了检索脚本的父目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用卸载程序脚本来删除安装脚本的父文件夹.

I'm working on an uninstaller script to delete the parent folder where the script is installed.

/usr/local/Myapplication/Uninstaller/uninstall.sh

因此,uninstall.sh必须这样做:

So uninstall.sh has to do this:

rm- rf /usr/local/Myapplication

我可以检索卸载所在的文件夹

I can retrieve the folder where uninstall resides

SYMLINKS=$(readlink -f "$0")
UNINSTALL_PATH=$(dirname "$SYMLINKS")

但是我仍然不确定获取父路径的漂亮方法.我曾想过使用sed来简化此路径的卸载程序"部分,但是是否有一种优雅的方法来获取Myapplication文件夹的路径以将其删除?

But I'm still unsure of the pretty way to get the parent path. I thought of using sed to demove the "Uninstaller" part of this path, but is there an elegant way to get the path to Myapplication folder to delete it?

谢谢

推荐答案

两次使用 dirname 怎么样?

APP_ROOT="$(dirname "$(dirname "$(readlink -fm "$0")")")"

报价灾难仅是为了防止路径中的空格是必需的.否则会更令人赏心悦目:

The quoting desaster is only necessary to guard against whitespace in paths. Otherwise it would be more pleasing to the eye:

APP_ROOT=$(dirname $(dirname $(readlink -fm $0)))

这篇关于检索脚本的父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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