调用shell中设置环境变量的最佳方式 [英] Best way to set environment variables in calling shell

查看:216
本文介绍了调用shell中设置环境变量的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要设置和使用上下文的实用程序。我想让它在shell中使用环境变量,以便它可以记住当前在调用之间的内容。理想情况下,我想从实用程序本身中设置这个环境变量。如下所示:

  mytool set-context< context> 
mytool do-stuff#意识到< context>

我想要这样做:

  export MYTOOL_CONTEXT =< context> 
mytool do-stuff#意识到< context>

现在,程序实际上不可能在调用shell的环境中设置环境变量(涵盖在SO 此处)。但是,这是我想要的行为,我正在寻找一种近似或解决方法。



我想到的一些想法:




  • 输出正确的字符串,并期望用户设置变量(假设变量不是简单地设置为< context> ,而是从其派生的一些字符串)。这将看起来像 export MYTOOL_CONTEXT = $(mytool get-context-var< context>)

  • 输出完整命令设置它。这将看起来像 $(mytool set-context< context>),其中命令的输出实际上被执行(并且输出将像 export MYTOOL_CONTEXT =< context> )。

  • 保存到名称基于shell的PID的临时文件。这在大多数情况下会起作用,但删除文件不会自动发生,所以它可能只是在重新启动之前(即可能永远在许多机器上)。



其他任何想法?谢谢!



注意:上述示例使用BASH语法,但在大多数shell中都有等同的。

解决方案

澄清之前



POSIX shell( bash ksh 等),而Bourne shell允许您只在一个命令的命令行上设置环境变量:

  MYTOOL_CONTEXT = LT;上下文> mytool do-stuff 

MYTOOL_CONTEXT 设置为调用 mytool (仅)的环境变量。顺便说一下,大多数shell接受一个选项 -k (它不是由POSIX标准化),这意味着所有看起来像VAR = value的参数都被视为环境变量,即使它们出现后命令名称。这是一个好奇心,实际价值相当有限,这就是为什么它不是标准化的。



POSIX env 命令旨在允许您控制调用的命令的环境,尽管通常使用没有任何参数来列出当前环境。因此,您可以使用:

  env MYTOOL_CONTEXT =< context> mytool do-stuff 

env 在设置命令行中指定的环境变量之前,您可以执行诸如取消设置每个环境变量的事情(以便您获得对环境的完全控制)。



澄清后, h3>

如果要将环境设置为后续使用,则命令是要使用的。您可以创建一个文件 context ,其中包含要执行的命令来设置环境。然后你可以使用:

 。上下文

,文件的内容将在当前shell的上下文中执行,因此您可以设置环境变量等。Bash为命令提供了一个同义词 source 。它受到C shell的启发,C shell不提供命令,但提供 source 作为等价物。将在 $ PATH 上搜索命名为命令参数的文件,但不需要执行(可读性足够)。如果您提供额外的参数,则会在命令的持续时间内成为位置参数( $ 1 等)。还要注意,文件完成后,虚线文件创建的任何变量或函数都将保持有效(与正常脚本中的变量不同,脚本完成时会消失)。如果您不会污染用户的名称空间,则必须小心。


I'm writing a utility that needs to be able to set and use context. I'd like it to use environment variables in the shell so that it can remember what context it's currently in between invocations. Ideally, I'd like to set this environment variable from within the utility itself. Something like:

mytool set-context <context>
mytool do-stuff # Aware of <context>

I'd like this to behave like:

export MYTOOL_CONTEXT=<context>
mytool do-stuff # Aware of <context>

Now, it's not actually possible for a program to set environment variables in the environment of the calling shell (covered on SO here). However, that's the sort of behavior I want, and I'm looking for an approximation or workaround.

Some ideas I had in mind:

  • Output the proper string, and expect the user to set the variable (assuming that the variable isn't simply set to <context>, but instead to some string derived from it). That would look something like export MYTOOL_CONTEXT=$(mytool get-context-var <context>).
  • Output the full command for setting it. That would look something like $(mytool set-context <context>), where the output of the command is actually executed (and the output would be something like export MYTOOL_CONTEXT=<context>).
  • Save to a temporary file whose name is based on the PID of the shell. This would work in most cases, but removing the file wouldn't happen automatically, so it would probably just lie around until reboot (ie, possibly forever on many machines).

Any other ideas? Thanks!

Note: The above examples use BASH syntax, but there are equivalents in most shells.

解决方案

Before clarification

The POSIX shells (bash, ksh, etc) and the Bourne shell allow you to set environment variables on a command line for one command only:

MYTOOL_CONTEXT=<context> mytool do-stuff

This sets MYTOOL_CONTEXT as an environment variable for that invocation of mytool (only). Incidentally, most shells accept an option -k (which is not standardized by POSIX) that means all arguments that look like VAR=value are treated as environment variables, even when they appear after the command name. This is a curiosity with rather limited practical value, which is why it is not standardized.

The POSIX env command is designed to allow you to control the environment of an invoked command, though it is commonly used without any arguments to list the current environment. So, alternatively, you might use:

env MYTOOL_CONTEXT=<context> mytool do-stuff

The advantage of env is that you can do things like unset every environment variable before setting the ones specified in the command line (so you get total control over the environment).

After clarification

If the intention is to set the environment for subsequent use, then the . command is the one to use. You can create a file, context, which contains commands to be executed to set the environment. Then you can use:

. context

and the contents of the file will executed in the context of the current shell, so you can set environment variables, etc. Bash provides a synonym, source, for the . command. It was inspired by the C shell which does not provide the . command but does provide source as an equivalent. The file named as an argument to the . command will be searched for on $PATH but does not need to be executable (readable is sufficient). If you provide extra arguments, they become the positional parameters ($1, etc) for the duration of the . command. Note too that any variables or functions created by the dotted file remain in effect when the file is finished (unlike variables in a normal script which vanish when the script completes). If you aren't going to pollute the user's name space, you have to be careful.

这篇关于调用shell中设置环境变量的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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