gpt4 book ai didi

linux - 如何在字符串中的任意位置随机添加一个特殊字符?狂欢

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:31 27 4
gpt4 key购买 nike

我有这个脚本可以生成 8-16 之间的随机字符。我对如何从银行 [! @ # $ % ^ & * ( ) _ + ] 这个字符串中的任意位置?

if [ $# -eq 0 ] then    
pwdlen=$(((RANDOM % 9 ) +8))
spclen=$((RANDOM % 1))

char=(0 1 2 3 4 5 6 7 8 9 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 A B C D E F G H I J K L M N O P Q R S T U V X W Y Z)

chars=(~ ! @ # $ % ^ & * - +)
#rand2=$random % 11

max=${#char[*]}
for i in `seq 1 $pwdlen`
do
let "rand=$RANDOM % $max"
str="${str}${char[$rand]}"
done
echo $str
exit 0
fi

最佳答案

teststring=foobarspam

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

randomchar=${specialchars:RANDOM % ${#specialchars}:1}
randompos=$(( RANDOM % ( ${#teststring} + 1 ) ))
newstring=${teststring:0:randompos}${randomchar}${teststring:randompos}

关于linux - 如何在字符串中的任意位置随机添加一个特殊字符?狂欢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558020/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com