0

非常に具体的な名前の複数のテスト ディレクトリがあり、それらのディレクトリ内に、複数のサブ ディレクトリを持つ複数のサブ ディレクトリがあります。

例:

*./V01A1A01/S01/R00/JADE/Anytest.drec
*./V01A1A01/S01/R01/JADE/Anytest.drec (サブディレクトリ R01 が異なることに注意)

等...

*./V01A1A01/S02/R00/JADE/Anytest.drec
*./V01A1A01/S02/R01/JADE/Anytest.drec (S02 とサブディレクトリ R01 が異なることに注意)

等...

Anytest.drecすべてのディレクトリを、時々変更するソース ファイル "Anytext.drec" (path=/home/mike/Desktop/Active_Test/Source_Files/Anytest.drec)に置き換えて名前を変更したいと考えています。Anytest.drec が V01_A1_A01_S01_R00.drec.

これまでの私のコードは次のとおりです。

#!/bin/bash 

path1=/home/mike/Desktop/Active_Test/Source_Files/Anytest.drec  #set path of source Anytest.drec file
set -x  #allows me to see what things are going on with code
echo "Please enter the Master folder you wish to search or update"   #asks user for the input folder to search
read "folder"       #creates variable "$folder" and sets the directory I want to search
jade_files=`find /home/mike/Desktop/Active_Test/$folder -name "*.drec" -print`

#finds all filenames in var $folder with the extension of .drec and puts the result in var $jade_files which now = /home/mike/Desktop/Active_Test/V01A1A02/S00/R00/JADE/Anytest.drec
path=`dirname "$jade_files"`  #dirname gives full paths of .drec files and puts result into var $path 
ext=${jade_files##*.}       #uses the var $jade_file to get the extension of .drec files found in the find search which = drec
basename=`basename "$jade_files"`  #gets the basenames from var $file which = Anytest.drec

#here's where my problems start... I'm trying to use the content of var "$jade_folders" to cp /Source_Files/Anytest.drec into var "$jade_folders" path(s) and using awk to parse out the portion of the dirname want to reconfigure into the filename
while read -r line; do   
    new_name=| `awk '{print $5, "_", $6, "_", $7 }'`
    echo $new_name
    cp -b /home/mike/Desktop/Active_Test/Source_Files/"$new_name"."$ext" "$line"
done <<< "$jade_folders"  #essentially, this var is the input to the while loop and <<< is how it's done.

私が得たものはこれです:

cp: cannot stat ‘/home/mike/Desktop/Active_Test/Source_Files/.drec’: No such file or directory

このスクリプトを修正して、希望どおりに動作させるにはどうすればよいですか?

わかりました、これは私が最終的に使用した修正です。すべてのアンダースコアを配置するわけではありませんが、ほとんどを配置します。残りの部分は、さらに学習しながら修正します。ありがとうエド:

    #!/bin/bash 
    path1=/home/mike/Desktop/Active_Test/Source_Files/Anytest.drec
    set -x
    echo "Please enter the Master folder you wish to search or update"
    read "folder"
    jade_files=`find /home/mike/Desktop/Active_Test/$folder -name "*.drec" -print`
    path=`dirname "$jade_files"`
    ext=${jade_files##*.}
    basename=`basename "$jade_files"` 
    cp -b "$path1" "$jade_files"."$ext"
4

1 に答える 1

1

あなたが何をしようとしているのかわからないので、まずスクリプトの構文をクリーンアップしてから、目的どおりに動作するかどうかを確認しましょう。そうでない場合は、何が問題なのか教えてください.

これを試して:

echo "Please enter the Master directory you wish to search or update"
read dir
jade_files=$(find /home/mike/Desktop/Active_Test/"$dir" -name '*.drec' -print)

while IFS= read -r line; do   
    path=$(dirname "$line")
    ext="${line##*.}"
    basename=$(basename "$line")  
    new_name=$(echo "$basename" | awk '{print $5, "_", $6, "_", $7 }')
    echo "$new_name"
    cp -b /home/mike/Desktop/Active_Test/Source_Files/"${new_name}.${ext}" "$line"
done <<< "$jade_files"

特に new_name の設定は、それがあなたがやろうとしていたことの単なる推測です。

上記はあなたが望むことをしますか?そうでない場合、何を別の方法で行う必要がありますか?

于 2014-09-06T20:22:06.743 に答える