Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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
Arrays 将多个文件重命名为随机字符串只会重命名一个文件_Arrays_String_Bash_Shell_Random - Fatal编程技术网

Arrays 将多个文件重命名为随机字符串只会重命名一个文件

Arrays 将多个文件重命名为随机字符串只会重命名一个文件,arrays,string,bash,shell,random,Arrays,String,Bash,Shell,Random,尝试将文件重命名为随机字符串。为了生成一个随机字符串,我从一个答案中提取了一些代码 #!/bin/bash chars=( {a..z} {A..Z} {0..9} ) function rand_string { local c=$1 ret= while((c--)); do ret+=${chars[$((RANDOM%${#chars[@]}))]} done printf '%s\n' "$ret" } output=$(rand_

尝试将文件重命名为随机字符串。为了生成一个随机字符串,我从一个答案中提取了一些代码

#!/bin/bash

chars=( {a..z} {A..Z} {0..9} )

function rand_string {
    local c=$1 ret=
    while((c--)); do
        ret+=${chars[$((RANDOM%${#chars[@]}))]}
    done
    printf '%s\n' "$ret"
}

output=$(rand_string 10)
为了练习,我在
$HOME/practice
中创建了一个目录,其中包含一些纯文本文件

/Users/me/practice/testfile1.txt
/Users/me/practice/testfile2.txt
/Users/me/practice/testfile3.txt
当尝试将这些文件重命名为随机字符串时,我没有得到3个随机名称,而是得到1个重命名为随机字符串的文件

for file in $HOME/practice/*
do
    mv "$file" $HOME/practice/"$output" 
done

#result
/Users/me/practice/i6TP3wiMDD
mv“$file”~/practice/“$output”
替换为
echo“$file”$output”
显示随机字符串在每个文件后重复,而不是为每个文件生成新的随机字符串

/Users/me/practice/testfile1.txt i6TP3wiMDD
/Users/me/practice/testfile2.txt i6TP3wiMDD
/Users/me/practice/testfile3.txt i6TP3wiMDD
我的问题分为两部分:

  • 为什么只有一个文件被重命名
  • 如何为重命名的每个文件生成新的随机字符串

  • 我还要说,上面的随机字符脚本超出了我目前的理解范围。我知道它可以生成随机字符。但我对它的内部运作仍有点不清楚

    问题是您只生成随机值的实例,并将其存储在
    输出
    变量中,并使用该变量重命名循环中的所有文件。在定义she-bang(
    #!/bin/bash
    )之后,使用
    set-x
    选项运行shell脚本扩展

    +++ rand_string 10
    +++ local c=10 ret=
    +++ (( c-- ))
    +++ ret+=O
    +++ (( c-- ))
    +++ ret+=k
    +++ (( c-- ))
    +++ ret+=H
    +++ (( c-- ))
    +++ ret+=Q
    +++ (( c-- ))
    +++ ret+=1
    +++ (( c-- ))
    +++ ret+=u
    +++ (( c-- ))
    +++ ret+=9
    +++ (( c-- ))
    +++ ret+=4
    +++ (( c-- ))
    +++ ret+=c
    +++ (( c-- ))
    +++ ret+=C
    +++ (( c-- ))
    +++ printf '%s\n' OkHQ1u94cC     # <--- See the same random file-names used throughout the files
    ++ output=OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv 1.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv 2.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv 5000.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv 5100.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv abc.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv file.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv final.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv ini1.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv ini2.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv listing.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv names.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv parameters.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv properties.txt OkHQ1u94cC
    ++ for file in '*.txt'
    ++ mv result.txt OkHQ1u94cC
    
    #!/bin/bash
    
    for file in $HOME/practice/*
    do
        mv "$file" $HOME/practice/"$(rand_string 10)"   # <-- Here the random string length can be controlled as you need.
    done
    
    +++ rand_string 10
    +++ local c=10 ret=
    +++ (( c-- ))
    +++ ret+=7
    +++ (( c-- ))
    +++ ret+=j
    +++ (( c-- ))
    +++ ret+=U
    +++ (( c-- ))
    +++ ret+=K
    +++ (( c-- ))
    +++ ret+=l
    +++ (( c-- ))
    +++ ret+=V
    +++ (( c-- ))
    +++ ret+=l
    +++ (( c-- ))
    +++ ret+=6
    +++ (( c-- ))
    +++ ret+=8
    +++ (( c-- ))
    +++ ret+=8
    +++ (( c-- ))
    +++ printf '%s\n' 7jUKlVl688
    ++ output=7jUKlVl688
    ++ for file in '*.csv'
    +++ rand_string 10
    +++ local c=10 ret=
    +++ (( c-- ))
    +++ ret+=N
    +++ (( c-- ))
    +++ ret+=B
    +++ (( c-- ))
    +++ ret+=O
    +++ (( c-- ))
    +++ ret+=p
    +++ (( c-- ))
    +++ ret+=j
    +++ (( c-- ))
    +++ ret+=5
    +++ (( c-- ))
    +++ ret+=T
    +++ (( c-- ))
    +++ ret+=b
    +++ (( c-- ))
    +++ ret+=S
    +++ (( c-- ))
    +++ ret+=R
    +++ (( c-- ))
    +++ printf '%s\n' NBOpj5TbSR
    ++ mv 1.csv NBOpj5TbSR                  # <- Unique file names generated.
    +++ rand_string 10
    +++ local c=10 ret=
    +++ (( c-- ))
    +++ ret+=N
    +++ (( c-- ))
    +++ ret+=R
    +++ (( c-- ))
    +++ ret+=Y
    +++ (( c-- ))
    +++ ret+=C
    +++ (( c-- ))
    +++ ret+=C
    +++ (( c-- ))
    +++ ret+=X
    +++ (( c-- ))
    +++ ret+=L
    +++ (( c-- ))
    +++ ret+=0
    +++ (( c-- ))
    +++ ret+=e
    +++ (( c-- ))
    +++ ret+=l
    +++ (( c-- ))
    +++ printf '%s\n' NRYCCXL0el
    ++ mv 2.csv NRYCCXL0el                    # <- Unique file names generated.