bash 确保所有文件夹都是使用匹配行从文件创建的

bash 确保所有文件夹都是使用匹配行从文件创建的

在下面,bash我试图确保创建了给定目录中的所有文件夹(由 $folders 表示)。在文件中,f1主文件夹将位于某处(将是多个主文件夹)。找到该文件夹​​后(由 $S5 表示),内容将$2打印到$STRING。Anewline始终分隔一个块,如果$STRING与相同,$folders则该文件夹已在目录中创建,可以跳过(大多数情况下都是这种情况)。如果$STRING与不同,$folders则会在目录中创建缺失的内容。代码似乎按预期工作,直到比较和实际匹配是下面的部分未按预期工作并打印并创建错误的文件夹。谢谢 :)。

f1

yyy_009 00-0000-xxx-xxx-xxx
yyy_004 00-0011-xxx-xxx-xxx
R_2019_00_00_00_00_00_xxxx_x0-00000-200_yyyyyyy

zzzz_0000 00-3333-zz-zz
zzzz_0005 00-1111-xxx-xxx
zzzz_0003 00-2222-yyy-yy
R_2019_00_00_00_00_00_xxxx_x0-00000-123-v5.6_xxx_xxx_xxx_xxx_xxx_xxx_xxxx

狂欢

DIR=/home/user/Desktop/new
for RDIR in "$DIR"/R_2019* ; do  ## # matching "R_2019*" to operate on desired directory and expand
  S5=${RDIR##*/}  ## store run with no path as S5
  TRIMSTR=${RDIR%%-v5.6*}  ## trim folder match in RDIR from -v5.6 and store in TRIMSTR
    mv "$RDIR" "$TRIMSTR"  ## move trimmed folder to directory
  cd "$TRIMSTR"
   folders=`ls -d *[0-9]*/`  ## list folders
    STRING=$(awk -F '\n' -v RS="" -v ref="$S5" '$0 ~ ref {print $2}' "$DIR"/f1)
  if [[ $folders != $STRING* ]] ## only execute folder on mis-match
   then
     mkdir -p "$TRIMSTR"/$STRING/folder
  fi
done

期望

R_2019_00_00_00_00_00_xxxx_x0-00000-123
  00-1111-xxx-xxx
    folder
  00-2222-yyy-yy
    folder
  00-3333-zz-zz
    folder

set -x

+ for rdir in '"$dir"/R_2019*'
+ s5=R_2019_00_00_00_00_00_xxxx_x0-00000-123- 
v5.6_xxx_xxx_xxx_xxx_xxx_xxx_xxxx
+ trimstr=/home/cmccabe/folder/R_2019_00_00_00_00_00_xxxx_x0-00000-123
+ mv /home/cmccabe/folder/R_2019_00_00_00_00_00_xxxx_x0-00000-123- 
v5.6_xxx_xxx_xxx_xxx_xxx_xxx_xxxx 
/home/cmccabe/folder/R_2019_00_00_00_00_00_xxxx_x0-00000-123
+ cd /home/cmccabe/folder/R_2019_00_00_00_00_00_xxxx_x0-00000-123
++ ls --color=auto -d 00-1111-xxx-xxx/ 00-2222-yyy-yy/
+ folders='00-1111-xxx-xxx/
00-2222-yyy-yy/'
++ awk -F '\n' -v RS= -v ref=R_2019_00_00_00_00_00_xxxx_x0-00000-123- 
v5.6_xxx_xxx_xxx_xxx_xxx_xxx_xxxx '$0 ~ ref {print $2}' 
/home/cmccabe/folder/f1
+ string='zzzz_0005 00-1111-xxx-xxx'
+ [[ 00-1111-xxx-xxx/
00-2222-yyy-yy/ != zzzz_0005 00-1111-xxx-xxx* ]]
+ mkdir -p '/home/cmccabe/folder/R_2019_00_00_00_00_00_xxxx_x0-00000- 
123/zzzz_0005 00-1111-xxx-xxx/folder'

相关内容