【问题标题】:use a variable as part of another variables name使用一个变量作为另一个变量名的一部分
【发布时间】:2015-07-09 02:01:26
【问题描述】:

Bash 问题!

所以我有 n 个参数,对于每个参数,我想做一个 for 循环并分配变量来保存每个参数的特征。例如,我有一个脚本,它在一个连续的 while 循环中运行,并查看我的网络中的用户活动......这是我的问题的简单概述:

while true

for argument
do 
# build an array to hold times
"$user"_times =()
# set a boolean value
"$user"_boolean=true

     if [ ""$user"_boolean" = true ]
     then
     echo $user logged on 
     "$user"_times+=( timestamp )
     fi
 done 
done
exit 0

真正的脚本将查看用户活动,根据某些用户行为更新布尔值,并在数组中记录一些用户活动信息——但我正在努力让它工作,这样我就可以添加简单的肉。语法应该是什么?我很难使变量起作用。

【问题讨论】:

  • 请参阅Bash FAQ 006 了解有关 shell 中变量间接的讨论。

标签: linux bash shell unix


【解决方案1】:

由于 bash 不支持散列/映射,我会考虑使用 perl/python 来根据他们的散列 ID 存储用户活动。您还可以访问可变大小活动详细信息的向量,例如包含活动向量的用户 ID 哈希。

Python script to list users and groups

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2018-03-17
    • 2018-09-22
    相关资源
    最近更新 更多