在 Bash 中循环字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7300070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:00:02  来源:igfitidea点击:

Looping through alphabets in Bash

bashloopsfor-loop

提问by behzad.nouri

I want to mvall the files starting with 'x' to directory 'x'; something like:

我想把mv所有以“x”开头的文件放到目录“x”中;就像是:

mv path1/x*.ext path2/x

and do it for all alphabet letters a, ..., z

并对所有字母 a, ..., z 执行此操作

How can I write a bash script which makes 'x' loops through the alphabet?

如何编写一个 bash 脚本,使“x”在字母表中循环?

回答by Kamil Dziedzic

for x in {a..z}
do
    echo "$x"
    mkdir -p path2/${x}
    mv path1/${x}*.ext path2/${x}
done

回答by Mat

This should get you started:

这应该让你开始:

for letter in {a..z} ; do
  echo $letter
done

回答by Luis Mu?oz

here's how to generate the Spanish alphabet using nested brace expansion

这是使用嵌套括号扩展生成西班牙字母表的方法

for l in {{a..n},?,{o..z}}; do echo $l ; done | nl
1  a
 ...
14  n
15  ?
16  o
...
27  z

Or simply

或者干脆

echo -e {{a..n},?,{o..z}}"\n" | nl

If you want to generate the obsolete29 characters Spanish alphabet

如果要生成过时的29 个字符的西班牙字母表

echo -e {{a..c},ch,{d..l},ll,{m,n},?,{o..z}}"\n" | nl

Similar could be done for French alphabet or German alphabet.

对法语字母或德语字母也可以进行类似的操作。

回答by anishsane

Using rename:

使用rename

mkdir -p path2/{a..z}
rename 's|path1/([a-z])(.*)|path2//' path1/{a..z}*

If you want to strip-off the leading [a-z] character from filename, the updated perlexpr would be:

如果您想从文件名中去除前导 [az] 字符,更新后的 perlexpr 将是:

rename 's|path1/([a-z])(.*)|path2//' path1/{a..z}*

回答by Thanh Trung

With uppercase as well

也有大写

for letter in {{a..z},{A..Z}}; do
  echo $letter
done

回答by Alphons

This question and the answers helped me with my problem, partially.
I needed to loupe over a part of the alphabetin bash.

这个问题和答案部分地帮助了我解决我的问题。
我需要在 bash 中放大部分字母表

Although the expansion is strictly textual

虽然扩展是严格的文本

I found a solution:and made it even more simple:

我找到了一个解决方案:并使它更简单:

START=A
STOP=D
for letter in $(eval echo {$START..$STOP}); do
    echo $letter
done

Which results in:

结果是:

A
B
C
D

Hope its helpful for someone looking for the same problem i had to solve, and ends up here as well

希望它对寻找我必须解决的相同问题的人有所帮助,并且最终也在这里