如何在不使用硬编码字典的情况下,在 bash 中将字符串的每个字母向下或向上移动给定数量的字母?
最佳答案
你是说像 ROT13 这样的东西吗:
pax$ echo 'hello there' | tr '[a-z]' '[n-za-m]'
uryyb gurer
pax$ echo 'hello there' | tr '[a-z]' '[n-za-m]' | tr '[a-z]' '[n-za-m]'
hello there
对于想要提供任意旋转(0 到 26)的更通用的解决方案,您可以使用:
#!/usr/bin/bash
dual=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
phrase='hello there'
rotat=13
newphrase=$(echo $phrase | tr "${dual:0:26}" "${dual:${rotat}:26}")
echo ${newphrase}
关于bash - 如何将字符串的每个字母移动给定数量的字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6441260/