获取包含“typeset -r”的脚本时出现未知错误包含在命令替换中

发布于 2024-09-05 11:09:48 字数 1542 浏览 11 评论 0原文

我希望获取一个脚本,打印该脚本定义的变量的值,然后将该值分配给命令行上的变量,并使用包装源/打印命令的命令替换。这适用于 ksh88,但不适用于 ksh93,我想知道为什么。

$ cat typeset_err.ksh
#!/bin/ksh
unset _typeset_var
typeset -i -r _typeset_var=1
DIR=init # this is the variable I want to print

当在 ksh88(本例中为 AIX 6.1 机器)上运行时,输出如下:

$ A=$(. ./typeset_err.ksh; print $DIR)
$ echo $A
init

当在 ksh93(本例中为 Linux 机器)上运行时,输出如下:

$ A=$(. ./typeset_err.ksh; print $DIR)
-ksh: _typeset_var: is read only
$ print $A

($A 未定义)

以上是只是一个示例脚本。我希望完成的实际任务是获取一个为许多变量设置值的脚本,以便我可以仅打印其中一个值,例如 $DIR,并具有 $A 等于该值。我事先不知道 $DIR 的值,但我需要在执行不同的批处理脚本期间将文件复制到 $DIR 。因此,我的想法是获取脚本以定义其变量,打印我想要的变量,然后通过 $(...) 语法将该打印的输出分配给另一个变量。诚然,有点黑客行为,但我不想在批处理脚本的环境中获取整个子脚本,因为我只需要其中一个变量。

开头的 typeset -r 代码就是错误。我正在获取的脚本包含此内容是为了提供某种信号量,以防止脚本在环境中多次获取。 (真实脚本中有一个 if 语句,用于检查 _typeset_var = 1,如果已设置则退出。)所以我知道我可以将其取出并获取 $DIR code> 可以很好地打印,但问题的限制包括保留 typeset -i -r

在示例脚本中,我首先放置了一个 unset ,以确保 _typeset_var 尚未定义。顺便说一下,根据 ksh93 的 ksh 手册页,我确实知道不可能取消设置 typeset -r 变量。

有多种方法可以解决此错误。现在最喜欢的是不使用排版,而只是设置不带排版的信号量(例如_typeset_var=1),但是代码原样的错误仍然让我感到好奇,我想看看是否有人可以解释为什么会发生这种情况。

顺便说一句,我放弃的另一个想法是从包含的脚本中 grep 我需要的变量,然后打印该变量以将 $A 设置为;但是,变量(上例中的$DIR)可能会被设置为另一个变量的值(例如DIR=$dom/init),并且可能会定义其他变量脚本中较早的部分;因此,我需要获取整个脚本以确保定义所有变量,以便在获取时正确定义 $DIR

I wish to source a script, print the value of a variable this script defines, and then have this value be assigned to a variable on the command line with command substitution wrapping the source/print commands. This works on ksh88 but not on ksh93 and I am wondering why.

$ cat typeset_err.ksh
#!/bin/ksh
unset _typeset_var
typeset -i -r _typeset_var=1
DIR=init # this is the variable I want to print

When run on ksh88 (in this case, an AIX 6.1 box), the output is as follows:

$ A=$(. ./typeset_err.ksh; print $DIR)
$ echo $A
init

When run on ksh93 (in this case, a Linux machine), the output is as follows:

$ A=$(. ./typeset_err.ksh; print $DIR)
-ksh: _typeset_var: is read only
$ print $A

($A is undefined)

The above is just an example script. The actual thing I wish to accomplish is to source a script that sets values to many variables, so that I can print just one of its values, e.g. $DIR, and have $A equal that value. I do not know in advance the value of $DIR, but I need to copy files to $DIR during execution of a different batch script. Therefore the idea I had was to source the script in order to define its variables, print the one I wanted, then have that print's output be assigned to another variable via $(...) syntax. Admittedly a bit of a hack, but I don't want to source the entire sub-script in the batch script's environment because I only need one of its variables.

The typeset -r code in the beginning is the error. The script I'm sourcing contains this in order to provide a semaphore of sorts--to prevent the script from being sourced more than once in the environment. (There is an if statement in the real script that checks for _typeset_var = 1, and exits if it is already set.) So I know I can take this out and get $DIR to print fine, but the constraints of the problem include keeping the typeset -i -r.

In the example script I put an unset in first, to ensure _typeset_var isn't already defined. By the way I do know that it is not possible to unset a typeset -r variable, according to ksh93's man page for ksh.

There are ways to code around this error. The favorite now is to not use typeset, but just set the semaphore without typeset (e.g. _typeset_var=1), but the error with the code as-is remains as a curiosity to me, and I want to see if anyone can explain why this is happening.

By the way, another idea I abandoned was to grep the variable I need out of its containing script, then print that one variable for $A to be set to; however, the variable ($DIR in the example above) might be set to another variable's value (e.g. DIR=$dom/init), and that other variable might be defined earlier in the script; therefore, I need to source the entire script to make sure I all variables are defined so that $DIR is correctly defined when sourcing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

谎言 2024-09-12 11:09:48

它在 ksh93(版本 JM 93t+ 2009-05-01)中对我来说工作得很好。但是,如果我这样做:

$ . ./typeset_err.ksh
$ A=$(. ./typeset_err.ksh; print $DIR)
-ksh: _typeset_var: is read only

那么您可能会以某种方式在当前环境中获取该变量 typeset -r

It works fine for me in ksh93 (Version JM 93t+ 2009-05-01). If I do this, though:

$ . ./typeset_err.ksh
$ A=$(. ./typeset_err.ksh; print $DIR)
-ksh: _typeset_var: is read only

So it may be that you're getting that variable typeset -r in the current environment somehow.

狼亦尘 2024-09-12 11:09:48

试试这个

A=$(ksh -c "./typeset_err.ksh && print \$DIR")

A=$(env -i ksh -c "./typeset_err.ksh && print \$DIR")

Try this

A=$(ksh -c "./typeset_err.ksh && print \$DIR")

or

A=$(env -i ksh -c "./typeset_err.ksh && print \$DIR")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文