简体   繁体   English

使用变量“值”在KornShell中定义变量“名称”

[英]Using variable 'values' to define variable 'names' in the KornShell

My program involves a number of JAVA threads calling a KornShell (ksh) script file with different parameters. 我的程序涉及许多JAVA线程,它们使用不同的参数调用KornShell(ksh)脚本文件。 This script file in turn calls some SQL commands which return some status. 该脚本文件又调用一些返回某些状态的SQL命令。 I store this status in a variable as a part of the script and check the value returned from SQL and take appropriate actions. 我将此状态存储在脚本中作为变量的一部分,并检查从SQL返回的值并采取适当的措施。 The problem is that all scripts are being executed in the same session and thereby the variable name gets shared across all threads. 问题在于所有脚本都在同一会话中执行,因此变量名将在所有线程之间共享。 Since these threads are working asynchronously, I want this link to be broken. 由于这些线程是异步工作的,因此我希望此链接断开。 The easiest solution that I could come up with was to have a different variable for each thread (use some input parameter to the script as an identifier) but this would mean that the name of that variable needs to be dynamic. 我能想到的最简单的解决方案是为每个线程使用不同的变量(使用脚本的某些输入参数作为标识符),但这意味着该变量的名称需要动态。 That is, I should be able to use the value of a variable in the name of another variable. 也就是说,我应该能够在另一个变量的名称中使用变量的值。 Is this even possible? 这有可能吗? If it is then how do I do it ? 如果是,那我该怎么办?

Java Thread - | Java线程-| Same session | 同一会话| | | | | Java Thread - - | Java线程--| Same Script | 相同脚本 | | | | Java Thread - - - | Java线程---| Same var name | 相同的var名称| | | | |
Java Thread - - | Java线程--| (This same var | | name can cause | Java Thread - | Inconsistencies)| (此相同的var | |名称可能会导致| Java线程-|不一致)|

If not, then can anyone suggest an alternative approach? 如果没有,那么有人可以建议替代方法吗? An important aspect of this is the far that the JAVA part is a server program and will be running for extended durations so I will need to 'unset' these variables manually. 其中一个重要方面是JAVA部分是服务器程序,并且将长时间运行,因此我将需要手动“取消设置”这些变量。 (Something I can't do if i am using an array to store all of these) (如果我使用数组存储所有这些,我将无法执行某些操作)

... I store this status in a variable as a part of the (ksh) script and check the value returned from SQL and take appropriate actions. ...我将此状态存储在(ksh)脚本的一部分中的变量中,并检查从SQL返回的值并采取适当的措施。 The problem is that all scripts are being executed in the same session and thereby the variable name gets shared across all threads. 问题在于所有脚本都在同一会话中执行,因此变量名将在所有线程之间共享。

So to summarize: 总结一下:

  • each thread uses System.exec(...) or equivalent to run a ksh script 每个线程使用System.exec(...)或同等功能来运行ksh脚本

  • the ksh script uses a variable to store the status from running an SQL query. ksh脚本使用变量来存储运行SQL查询的状态。

In this scenario, the ksh variable will NOT be shared between the different instances of the running script. 在这种情况下,将不在运行脚本的不同实例之间共享ksh变量。 Each ksh instance has its own variable set. 每个ksh实例都有其自己的变量集。

There must be another explanation for what is going on here. 对于这里发生的事情,必须有另一种解释。


You say that if [ $var_$val -eq 0 ]; then ... 您说, if [ $var_$val -eq 0 ]; then ... if [ $var_$val -eq 0 ]; then ... gives the wrong answer. if [ $var_$val -eq 0 ]; then ...给出错误的答案。 I'm not surprised. 我不惊讶。 $var_$val cannot possibly expand to a number, so the test to see if it is zero will always fail. $var_$val可能无法扩展为数字,因此查看其是否为零的测试将始终失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM