Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何在字符串中的任意位置随机添加一个特殊字符?猛击_Linux_Bash_Unix - Fatal编程技术网

Linux 如何在字符串中的任意位置随机添加一个特殊字符?猛击

Linux 如何在字符串中的任意位置随机添加一个特殊字符?猛击,linux,bash,unix,Linux,Bash,Unix,我有一个脚本,它生成一个8-16之间的随机字符。我不知道如何在这个字符串的任意位置随机添加一个来自银行的单个随机特殊字符 如果[$#-eq 0],则 pwdlen=$((随机%9)+8)) spclen=$(随机%1)) char=(01 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 t u w x y z a b c d e f g h i j k l m n o p q r t u v w y z) 字符=(~!@$%^&*-+) #

我有一个脚本,它生成一个8-16之间的随机字符。我不知道如何在这个字符串的任意位置随机添加一个来自银行的单个随机特殊字符

如果[$#-eq 0],则
pwdlen=$((随机%9)+8))
spclen=$(随机%1))
char=(01 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 t u w x y z a b c d e f g h i j k l m n o p q r t u v w y z)
字符=(~!@$%^&*-+)
#rand2=$random%11
max=${#char[*]}
对于i,按顺序1$pwdlen`
做
让“rand=$RANDOM%$max”
str=“${str}${char[$rand]}”
完成
echo$str
出口0
fi

我不确定您使用的是什么脚本语言。我使用PHP为您编写了一个解决方案。如果您使用的不是PHP,那么您应该能够将相同的逻辑转换为其他语言,并获得相同的结果

<?php
//This is the original string where you want to add a random character to
$org_string = 'This is My original String';
//calculates the length of the string
$org_length = strlen($org_string);
//find a random position
$pos = rand(0, $org_length-1);

//concatenate the first part of the string, random character, the remaining string
$final = substr($org_string, 0, $pos) . getOne() . substr($org_string, $pos);

//print the final value
echo $final;

//return a random string
function getOne(){
    //the following string is 12 characters in length. it is all available characters that you want to select from
    $str = '!@#$%^&*()_+';
    //return a random character
    return $str[rand(0, 11)];
}

?>

您可以使用以下代码

#!/bin/bash

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
    A=$(echo $str|wc -c) ## To get the count of 
    P=$((RANDOM % $A)) ## To get a random position of where to insert the character.
    I=$((RANDOM % 11)) ## To get a random index number of chars array
    C=${chars[$#]}
    echo $str | sed 's!^\(.\{'$P'\}\).!\1\'"$C"'!'  ## Inserting the special character to string in defined position
    exit 0
fi
输出:

$ for i in `seq 1 10`;do ./test1;done
j^eh8BmD2H
0B01^1AN6EVw
Wu2$LLTILuDN8fSV
e^90gmHjksDo
eB7wa\#fmwf
NVAtJkmfqx~
JaHvD%uyO3rB
ncFrgyyz~UkZ
q0LLRHUNATM8DL
X%ARcXgyC1Do
$ for i in `seq 1 10`;do ./test1;done
j^eh8BmD2H
0B01^1AN6EVw
Wu2$LLTILuDN8fSV
e^90gmHjksDo
eB7wa\#fmwf
NVAtJkmfqx~
JaHvD%uyO3rB
ncFrgyyz~UkZ
q0LLRHUNATM8DL
X%ARcXgyC1Do