linux - 运行for循环以在shell脚本中重命名多个文件时出错

标签 linux unix for-loop rename aix

在 AIX 中使用 for 循环重命名多个文件时出现错误

${fn/$eisinno/$efilename}": 0403-011 The specified substitution is not valid for this command.

输入文件:

raj_10576_INE728J01019_arya1.pdf

ram_10576_INE728J01019_arya1.pdf

rhaul_10576_INE728J01019_arya1.pdf

sanjay_10576_INE728J01019_arya1.pdf

dinesh_10576_INE728J01019_arya1.pdf

所需的输出文件:

raj_10576_Remote_sag.pdf

ram_10576_Remote_sag.pdf

rhaul_10576_Remote_sag.pdf

sanjay_10576_Remote_sag.pdf

dinesh_10576_Remote_sag.pdf

我的脚本如下:

#!/bin/bash

eisinno="INE728J01019_arya1.pdf"

evenno=10576

efilename="remote_sag.pdf"


cd /home/rishabh/$eveno

for file in *_$eveno_*.pdf

do
    mv -i "${file}" "${file/$eveno_$eisinno/$eveno_remote_$efilename}"

done

请帮助我

最佳答案

evenno 中使用双 n 并使用大括号确保变量结束的位置:

#!/bin/bash
eisinno="INE728J01019_arya1.pdf"
evenno=10576
efilename="remote_sag.pdf"

cd /home/rishabh/${evenno}
for file in *_${evenno}_*.pdf; do
   echo "Debug: ${file} ==> ${file/${evenno}_${eisinno}/${evenno}_remote_${efilename}}"
   # Alternative: 
   echo ${file} | sed "s/${evenno}_${eisinno}/${evenno}_remote_${efilename}/"

   mv -i "${file}" "${file/${evenno}_${eisinno}/${evenno}_remote_${efilename}}"
done

关于linux - 运行for循环以在shell脚本中重命名多个文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40056136/

相关文章:

php - 从远程服务器下载文件,下载完成后删除

c++ - 计算字符串 C++ 中出现的次数?

java - 一个单词中元音的数量

r - 关于lapply的使用

MYSQL C 连接器

linux - 自动关闭预定的 Linux 机器

unix - unix中静态库和动态库的区别

linux - 编写 shell 脚本将目录中最旧的文件移动到新目录。?

c - 编译 .c 文件时,出现以下错误 : error: array type has incomplete element type

linux - UBoot波特率参数