Shell脚本-迭代空格分隔的单词/字符(以zsh为单位)
我在理解如何在shell脚本中迭代空格分隔的单词/字符时遇到了一些困难。例如,我想迭代一个变量,该变量包含字母表中由空格分隔的字符 注意:即使字母表变量包含空格分隔的字符串而不是字符,即“aa bb cc…”而不是“a b c…”,结果也应相同 我尝试了以下提供的许多替代方案: 示例:Shell脚本-迭代空格分隔的单词/字符(以zsh为单位),shell,iteration,zsh,Shell,Iteration,Zsh,我在理解如何在shell脚本中迭代空格分隔的单词/字符时遇到了一些困难。例如,我想迭代一个变量,该变量包含字母表中由空格分隔的字符 注意:即使字母表变量包含空格分隔的字符串而不是字符,即“aa bb cc…”而不是“a b c…”,结果也应相同 我尝试了以下提供的许多替代方案: 示例: 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
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
预期/期望输出:
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
结果:
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
附加测试(未成功):
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])?试试这个
IFS=$' \t\n'
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
希望能有所帮助谢谢你的帮助。由于您的反馈,我发现了错误 当我发布这个问题时,我认为这是无关紧要的,但我正在试验.zshrc文件中的函数。因此,我使用(只是我的假设)zsh解释器,而不是sh或bash解释器 意识到这可能是一个潜在的问题,我在谷歌上搜索并发现了以下内容 因此,我测试了以下各项,并按预期工作:
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中似乎没有我预期的效果
。。。我本应使用的小细节:
((++index))
or
((index++))
instead of
index=$((++$index))
你的代码应该可以工作。唯一的原因是如果您将变量
IFS
设置为默认空白字符以外的其他字符,则不会出现此问题。我无法重现此问题。检查脚本中的IFS
作业。我也不;一切正常,一切正常。无法重现该问题。@Barmar您是对的,我应该帮助他编写代码,而不是提供链接。IFS的默认值由空格字符组成(准确地说:空格、制表符和换行符)。除非Barmar说您的IFS不是空格、制表符或换行符,否则您的代码应该可以工作。请尝试打印IFS echo“$IFS”的值!谢谢你的建议,但是我针对我的特殊问题测试了它,但没有成功。