简体   繁体   English

bash脚本中的模糊字符串

[英]Obscure string in bash script

I am trying to do the following:我正在尝试执行以下操作:

obscure ( ) {
    local txt="$1"
    echo "$txt" | tr '[:alnum:]' '*'
}

So that if I do:所以如果我这样做:

obscure 'mysecretstring'

I get:我得到:

**************

What matcher can I use for tr , instead of [:alnum:] to mean 'any character'?我可以为tr使用什么匹配器,而不是[:alnum:]来表示“任何字符”?

Is there a better way to implement obscure ?有没有更好的方法来实现obscure Another option that comes to mind is sed .想到的另一个选项是sed

You can use pure BASH:您可以使用纯 BASH:

obscure() {
   local txt="$1"
   echo "${txt//?/*}"
}

"${txt//?/*}" will replace each character in $txt by * "${txt//?/*}"$txt的每个字符替换为*

Test it:测试一下:

obscure 'mysecretstring3123213213'
************************

obscure mysecretstring
**************

obscure '!@#$%^&*()_+=-'
**************

obscure '中文版'
***

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM