Shell脚本-遍历空格分隔的单词/字符(在zsh中) [英] Shell script - iterate over space separated words/characters (in zsh)

查看:752
本文介绍了Shell脚本-遍历空格分隔的单词/字符(在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.

通过意识到这可能是一个潜在问题,我在Google上搜索并找到了以下

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆