ksh:使用循环计数器(语法问题)定义循环中的变量
#!/bin/ksh
today=$(date "+%b %e")
homedir=/home/
for dir in jim bob joe; do
rootfiles_today_[$dir]=$(ssh hostname "ls -lrt $homedir$dir" | grep root | grep "$today")
printf "\n root owns the following files in $dir "
printf "\n\tSize\tDate\tTime\tFile Name\n\n"
printf "$rootfiles_today_[dir]" | awk '{printf "\t" $5 "\t" $6 " " $7 "\t" $8 "\t" $9 "\n"}'
done
当我尝试这样做时,输出给我
rootfiles_today_jim=: not find
,就好像变量名保留 = 而不是使用它来定义变量一样。我无法通过这一部分,涉及引号、[]、{}、set -A、eval(ksh88 没有定义)的每个变体,我能想到的要么不起作用并停止脚本,要么给出同上。
如何正确定义(动态?迭代?我不知道正确的术语)变量名称或说服它让 = 完成其工作?
其次,请确认我在定义后调用变量的方式将是 ${rootfiles_today_[dir]}
在循环内?循环结束后怎么样,如果我想向脚本添加更多内容并让这 3 个变量可以按名称调用,我将必须按它们的特定全名调用或 echo $@ | grep word 来调用特定的一个,或者(我正在摸索这个问题,我知道有很多方法可以做几乎任何事情)是否有另一种方法可以在后续循环中需要时迭代调用其中一个或全部?这更多的是次要问题,主要问题是定义一个有用的变量。
所有这些在 Bash 和较新的 shell 中都更简单,我的主要障碍往往是 ksh 中的不同或限制。
#!/bin/ksh
today=$(date "+%b %e")
homedir=/home/
for dir in jim bob joe; do
rootfiles_today_[$dir]=$(ssh hostname "ls -lrt $homedir$dir" | grep root | grep "$today")
printf "\n root owns the following files in $dir "
printf "\n\tSize\tDate\tTime\tFile Name\n\n"
printf "$rootfiles_today_[dir]" | awk '{printf "\t" $5 "\t" $6 " " $7 "\t" $8 "\t" $9 "\n"}'
done
When I try this, the output gives me
rootfiles_today_jim=: not found
as if the variable name is keeping hold of the = instead of using it to define the variable. I can't get past this part, each variation involving quotes, [], {}, set -A, eval, (ksh88 does not have define) that i can think of either doesn't work and stops the script, or gives the same above.
How can I properly define the (dynamic? iterative? I don't know the correct term) variable name or persuade it to let the = do its job?
Secondarily, please confirm my way to call the variable once defined is going to be ${rootfiles_today_[dir]}
inside the loop? What about after the loop is over, if I want to add more to the script and have these 3 vars i can call by name, I will have to either call by their specific full name or echo $@ | grep word
to call a specific one, or (I am fumbling this question, I know there are many ways to do almost anything) is there another way to call one or all of them iteratively if needed in a subsequent loop? This is more of a secondary ask, the main issue is getting a useful variable defined.
All of this is simpler in Bash and newer shells, my main stumbling blocks tend to be things that are different or limited in ksh.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论