shell 脚本中出现循环问题后变量仍然为空
我对 shell 脚本编写很陌生,并且遇到了一个非常复杂的问题。该程序相当简单,所以我将其发布在这里:
#!/bin/bash
list=""
list=`mtx -f /dev/sg2 status | while read status
do
result=$(echo ${status} | grep "Full")
if [ -z "$result" ]; then
continue
else
echo $(echo ${result} | cut -f3 -d' ' | tr -d [:alpha:] | tr -d [:punct:])
fi
done`
echo ${list}
for haha in ${list}
do
printf "current slot is:%s \n" ${haha}
done
该程序的作用是执行 mtx -f /dev/sg2 status 并转到每一行并查看是否有完整的磁盘。如果该行中包含“Full”,我将记录该行中的槽号,并将其放入列表
中。
请注意,我在第 6 行的 list=
之后放了一个反引号,它覆盖了之后的整个“while”循环。我不清楚原因,但我通过谷歌搜索得到了这个用法。据说 while 循环会打开一个单独的 shell 或类似的东西,所以当 while 循环完成时,你在循环中连接的任何内容都会丢失,所以在我最初的实现中, list
while 循环后仍然为空。
我的问题是:即使上面的代码工作正常,但对其他人来说看起来相当棘手,更糟糕的是,循环完成后我只能创建一个 list
。有没有更好的方法来解决这个问题,以便我可以从循环中提取更多信息?就像如果我需要 list2
来存储其他值怎么办?谢谢。
I'm very new in shell scripting, and I encountered a problem that is quite wired. The program is rather simple so I just post it here:
#!/bin/bash
list=""
list=`mtx -f /dev/sg2 status | while read status
do
result=$(echo ${status} | grep "Full")
if [ -z "$result" ]; then
continue
else
echo $(echo ${result} | cut -f3 -d' ' | tr -d [:alpha:] | tr -d [:punct:])
fi
done`
echo ${list}
for haha in ${list}
do
printf "current slot is:%s \n" ${haha}
done
What the program does is that it executes mtx -f /dev/sg2 status
and goes to each line and see if there's a full disk. If it has "Full" in that line, I'll record the slot number in that line, and put in the list
.
Notice that I put a back quote after list=
at line 6, and it covers the whole "while" loop after that. The reason is unclear to me, but I got this usage by just googling it. It is said that the while loop will open up a separate shell or something like that, so when the while loop is done, whatever you concatenated in the loop will get lost, so in my initial implementation, list
is still empty after the while loop.
My question is: even if the code above works fine, it looks pretty tricky to others, and what's worse, I can only make only ONE list
after the loop is done. Is there a better way to fix this so that I can pull out more information from the loop? Like what if I need list2
to store other values? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 shell 脚本确实有效。如果您想每行获取两条信息而不是一条信息,则必须更改此行
以连接由逗号或任何其他“特殊”字符分隔的所需值。然后你可以这样解析你的列表:
Your shell script does work. If you wanted to get two pieces of info per line insteal of one, you would have to change this line
to concatenate the desired values separated by a comma or any other "special" character. Then you could parse your list this way :
请参阅此说明。由于涉及管道,
while read...
代码不会在当前 shell 中执行,而是在子 shell 中执行(无法更新当前进程的子进程' (environment/shell )变量)。选择列出的解决方法之一,使
while read...
循环在当前 shell 中执行。See this explanation. As a pipe is involved, the
while read...
code isn't executed in your current shell, but in a subshell (A child process which can't update your current process' (environment/shell) variables).Choose on of the listed workarounds to make the
while read...
loop execute in your current shell.