Bash环境变量和查找安装目录
我有一个 Bash 脚本,它基本上初始化应用程序并设置参数。这些参数之一是 OpenOffice 的位置。现在,OpenOffice 在安装时不会设置环境变量。
查找已安装应用程序的位置并缓存该信息以便下次不必执行 I/O 的最佳方法是什么?
我的想法只是在 /usr/ 上运行一个 find 来查找具有特定文件的 OpenOffice 目录。当找到该目录时,将该目录存储在环境变量中,并在此脚本中检查环境变量是否已设置并且是一个目录,如果是,则使用它,如果不是,则再次搜索。
这将允许脚本在没有用户交互的情况下工作,但也允许用户自己设置路径(因为它是一个环境变量)。
这似乎是一种“不好的做法”,所以我希望其他人可以给我获取有关软件安装信息的常用方法。如果有帮助,OpenOffice 很可能会使用 aptitude 安装。
I have a Bash script that basically initializes an application and sets parameters. One of these parameters is the location to OpenOffice. Now OpenOffice doesn't set an environment variable when you install it.
What is the best method of finding the location of an application installed and caching that information so you don't have to do the I/O next time?
What I was thinking was simply running a find on /usr/ for the OpenOffice directory which has a specific file. When it's found store that directory in a environment variable and in this script check if the environment variable is set and is a directory, if so use it, if not search again.
This would allow the script to work without user interaction but also allow the user to set a path themselves (since it's an environment variable).
This seems like a "bad practice", so I'm hoping maybe someone else can give me the common way of getting information about a software install. If it helps, OpenOffice will most likely be installed using aptitude.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常看到的完成方式是使用如下代码:
然后您始终可以假设稍后在脚本中设置了 OO_EXE 环境变量。用户可以在运行脚本之前通过在环境中设置 OO_EXE 来覆盖默认值,但如果他不这样做,则脚本将回退到默认值。
如果您想在脚本第一次运行时动态查找默认值并每隔一段时间重用它,那么我会在脚本第一次运行时提示用户输入路径,在用户的主目录中放置一个点文件,然后每隔一段时间就读取该文件。
The way I've usually seen it done is with some code like this:
Then you can always assume that the
OO_EXE
environment variable is set later on in the script. The user can override the default by settingOO_EXE
in the environment before running the script, but if he doesn't then the script falls back to the default.If you want to dynamically find the default the first time the script is run and reuse it every other time, then I would prompt the user for the path the first time the script is run, drop a dot file in the user's home directory, and read from that file every other time.
许多应用程序在自行安装时都会设置此类值。例如,当用户运行您的应用程序的配置脚本时,用户可以指定 ooffice 的位置。脚本将使用该值或尝试查找它,或者如果用户未指定该值,则使用默认值。安装应用程序后,它将有一个硬编码值。
Many applications set such values when they install themselves. For example, when a user runs the configure script for your app, the user has the ability to specify the location of ooffice. The script will use that value or try to find it or use a default if the user leaves it unspecified. When the app is installed, it will have a hard-coded value.
您可以使用命令
which
。You could use the command
which
.