Shell 脚本 - 遍历空格分隔的单词/字符(在 zsh 中) [英] Shell script - iterate over space separated words/characters (in zsh)
问题描述
我在弄清楚如何在 shell 脚本中迭代空格分隔的单词/字符时遇到了一些麻烦.例如,我想遍历一个变量,该变量包含由空格分隔的字母表中的字符.
I am having some trouble figuring out how to iterate over space separated words/characters in a shell script. For instance I would like to iterate over a variable containing the characters in the alphabet separated by a space.
注意:即使字母表变量包含空格分隔的字符串而不是字符,结果也应该相同,即aa bb cc ..."而不是a b c .."
NOTE: The result should be the same even if the alphabet variable contained space separated strings instead of characters, i.e "aa bb cc ..." instead of "a b c .."
我已经尝试了很多从以下网站提供的替代方案:如何在bash中将一行拆分为由一个或多个空格分隔的单词?
I have tried a lot of the alternatives provided from: How to split a line into words separated by one or more spaces in bash?
示例:
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
预期/期望的输出:
1. a
2. b
3. c
and so on..
结果:
1. a b c d e f g h i j k l m n o p q r s t u v w x y z
附加测试(未成功):
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in ${alphabet[@]}; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local alphabetArray=( ${alphabet} )
local index="0"
for character in "${alphabetArray[@]}"; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local alphabetArray=( ${alphabet} )
local index="0"
for character in ${alphabetArray}; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
有人可以提供一个解决方案吗(我更喜欢在不显式使用索引变量的情况下迭代字母变量的解决方案,即 $alphabet[index] )?
Could someone provide a solution on how to solve this(I would prefer a solution that iterates the alphabet variable without explicitly using an index variable, i.e $alphabet[index] )?
推荐答案
感谢您的帮助.由于您的反馈,我发现了错误.
Thanks for your help. I discovered the error thanks to your feedback.
当我发布这个问题时,我认为这无关紧要,但我正在尝试我的 .zshrc 文件中的函数.因此,我使用的是(只是我的假设)zsh 解释器,而不是 sh 或 bash 解释器.
I thought that it was irrelevant when I posted this question but I was experimenting with functions in my .zshrc file. Hence I was using (just my assumption) the zsh interpreter and not the sh or bash interpreter.
意识到这可能是一个潜在的问题,我用谷歌搜索并找到了以下 如何在 zsh 中一次遍历一个单词
By realizing that this could be a potential problem, I googled and found the following How to iterate through string one word at a time in zsh
所以我测试了以下内容,它按预期工作:
So I tested the following and it works as expected:
setopt shwordsplit
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$(($index+1))
echo "$index. $character"
# Possibility to do some more stuff
done
unsetopt shwordsplit
注意:
index=$((++$index))
and/or
index=$(($index++))
在 zsh 中似乎没有像我预期的那样工作.
Doesn't seem to work as I expected in zsh.
...我应该使用的小细节:
... The little gritty details, I should have used:
((++index))
or
((index++))
instead of
index=$((++$index))
这篇关于Shell 脚本 - 遍历空格分隔的单词/字符(在 zsh 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!