Bash环境变量和查找安装目录

发布于 2024-08-14 07:16:54 字数 653 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

゛时过境迁 2024-08-21 07:16:54

我通常看到的完成方式是使用如下代码:

[ -z "$OO_EXE" ] && OO_EXE=/usr/bin/oo # or whatever the executable is called

然后您始终可以假设稍后在脚本中设置了 OO_EXE 环境变量。用户可以在运行脚本之前通过在环境中设置 OO_EXE 来覆盖默认值,但如果他不这样做,则脚本将回退到默认值。

如果您想在脚本第一次运行时动态查找默认值并每隔一段时间重用它,那么我会在脚本第一次运行时提示用户输入路径,在用户的主目录中放置一个点文件,然后每隔一段时间就读取该文件。

The way I've usually seen it done is with some code like this:

[ -z "$OO_EXE" ] && OO_EXE=/usr/bin/oo # or whatever the executable is called

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 setting OO_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.

浅语花开 2024-08-21 07:16:54

许多应用程序在自行安装时都会设置此类值。例如,当用户运行您的应用程序的配置脚本时,用户可以指定 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.

吲‖鸣 2024-08-21 07:16:54

您可以使用命令which

asafe@mimimi:~$ which openoffice.org 
/usr/bin/openoffice.org
asafe@mimimi:~$ 

You could use the command which.

asafe@mimimi:~$ which openoffice.org 
/usr/bin/openoffice.org
asafe@mimimi:~$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文