0

我有多个具有非常特定名称的测试目录,并且在这些目录中我有多个子目录和多个子目录。

例子:

*./V01A1A01/S01/R00/JADE/Anytest.drec
*./V01A1A01/S01/R01/JADE/Anytest.drec(注意子目录 R01 不同)

ETC...

*./V01A1A01/S02/R00/JADE/Anytest.drec
*./V01A1A01/S02/R01/JADE/Anytest.drec(注意 S02 和子目录 R01 现在不同了)

ETC...

我想用我Anytest.drec不时更改的源文件“Anytext.drec”(路径=/home/mike/Desktop/Active_Test/Source_Files/Anytest.drec)替换和重命名所有目录。我希望 /JADE 目录中的最终名称采用 dirname,以便 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 回答