我正在尝试创建一个脚本,该脚本将根据文件位置和文件类型来识别文件位置和类型。例如,如果我的位置是/home/file/txt/test.txt和/home/file/tcl/test.tcl。我想为第三个/位置设置变量。因此,在示例情况下,这将是txt和tcl。因此,我将获得遵循此格式的位置列表。我想通过列表进行foreach,然后将变量设置为第三个/位置。然后,我将打印该变量。正确的正则表达式是什么?
答案 0 :(得分:0)
您的任务很模糊。张贴所需的输入和输出总是好的。 无论如何,如果我对您的规范很了解的话,这是您的任务的启动:
#!/bin/env csh
set dir='./file/'
set types=('tcl' 'txt')
foreach type (${types})
#HERE replace with Your commands
#example find with regex
echo " ... searching for *.${type} in ${dir}${type}/"
find "${dir}${type}/" -type f -regex '.*\.'"${type}"'$' -print0 | xargs -0 printf "%s\n"
end
exit 0
存在dir
的设置,用于查找您的types
并排列数组。 Foreach循环遍历dir / type并使用regexp查找后缀类型的文件。 xargs
在此处是可选的,我故意将其放置以显示如何正确处理文件。您可以替换例如与:
xargs -0 -I {} cp {} /home/newdestination/
例如,如果您需要复制到新的目的地。
测试:
% ./list_file.csh
... searching for *.tcl in ./file/tcl/
./file/tcl/a.tcl
./file/tcl/b.tcl
... searching for *.txt in ./file/txt/
./file/txt/a.txt
./file/txt/b.txt