2016-10-05 19 views
1

ファイルの名前をランダムな文字列に変更しようとしています。私はランダムな文字列を生成するために、this questionへの答えからいくつかのコードを取った。複数のファイルの名前をランダムな名前に変更すると、1つのファイルの名前が変更されます。

#!/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でディレクトリを作りました。代わりに3人のランダムな名前を得るための、ランダムな文字列これらのファイルの名前を変更しようとすると

/Users/me/practice/testfile1.txt 
/Users/me/practice/testfile2.txt 
/Users/me/practice/testfile3.txt 

は、私が代わりにランダムな文字列に名前を変更1つのファイルが残っています。 echo "$file" "$output"mv "$file" ~/practice/"$output"の交換

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

#result 
/Users/me/practice/i6TP3wiMDD 

はランダムな文字列ではなく、すべてのファイルの新しいランダムな文字列を生成するすべてのファイルの後に繰り返されていることを私に示しています。

/Users/me/practice/testfile1.txt i6TP3wiMDD 
/Users/me/practice/testfile2.txt i6TP3wiMDD 
/Users/me/practice/testfile3.txt i6TP3wiMDD 

私の質問は2つの一部です:

  1. はなぜのみ1ファイルの名前が変更されていますか?
  2. 名前を変更するファイルごとに新しいランダムな文字列を生成するにはどうすればよいですか?

上記のランダムな文字のスクリプトは、私の現在の理解の上にあるとも言います。私はそれがランダムな文字を生成するために動作することを知っています。しかし、それの内部の動作はまだ私にはいくぶん不明です。

答えて

1

ランダムな値のインスタンスのみを生成し、それをoutput変数に格納し、同じものを使用してループ内のすべてのファイルの名前を変更しているという問題があります。 she-bang(#!/bin/bash)を定義した後にset -xオプションを指定して実行した場合のシェルスクリプト展開を参照してください。

+++ 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 

上記は私のサンプルデータです。最善の方法は、拡張時に関数を呼び出すことによって関数の名前を変更するためのforループを修正することです。

#!/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 

以下のように今以上の修正で、あなたは私の.csvファイルの実際の名前変更の私のスクリプトが

+++ 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. 
として展開見ることができます
関連する問題