简体   繁体   中英

Error While running for loop for renaming multiple file in shell script

While renaming multiple file in AIX using for loop I am getting error

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

Input File:

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

Desired Output File:

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

My script is as follow:

#!/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

Kindly help me

Use double n in the evenno s and use braces to make sure where a variable ends:

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM