我正在尝试重命名目录中的所有文件。例如,这些文件名:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2
应重命名为:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110111111.dat
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180111222222.dat
以 R51_1 结尾的文件应该有一个时间戳,而 R51_2 的所有文件都有另一个时间戳。这是代码:
#!/bin/ksh
x=$(date +%Y%m%d%H%M%S)
ls -lrt *BACMAIN* | cut -f9,10 -d'_' >tmp.txt
while read LINE; do
x=$(date +%Y%m%d%H%M%S)
filearray=`ls -1t *"$LINE"`
echo "${filearray[@]}"
for file in "${filearray[@]}"
do
mv "$file" "${file%_$LINE}"_$x.dat;
done
x=x++;
done < tmp.txt
...但是我收到如下错误:
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180112000002.dat_R52_1
BPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180112000002.dat_R52_1
mv: accessing `BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180112000002.dat_R52_1\nBPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180112000002.dat_20180112151217.dat': File name too long
BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180110000000.dat_R52_2
BPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180110000000.dat_R52_2
mv: accessing `BPP_xxx_xxx-ASTATR_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-ASTEVSRC_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-ASTMAIN_11_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACBILLDET_12_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACMAIN_12_xxx_1_1_20180110000000.dat_R52_2\nBPP_xxx_xxx-BACPAYDET_12_xxx_1_1_20180110000000.dat_20180112151217.dat': File name too long
答案 0 :(得分:0)
您为filearray
分配了一个常规字符串,但之后您将其用作数组。
你必须告诉shell,你想要一个数组
typeset -a filearray
或
filearray=( $(ls -1t *[12]) )
为了进行调试,您可以在echo
前放置mv
,如果您获得了所需的结果,请将其删除。 或定义debug=echo
,将$debug
放在关键命令前面。完成后,定义debug=
对于更多改进,将代码放在https://www.shellcheck.net/中,并处理第12行。