Skip to content
Advertisement

(answerd) find command in Bash SH is not working as expected [closed]

I have s script like below, which should use ‘find’ to copy all files end with extention (in the list I gave) to the destination directory. however, it’s not working. I tried to change ‘ to ”, ”’, “,””,””” in line start with ‘find’ but still no luck. could you please help to take a look, see what went wrong for my coding, thanks a lot.

#!/bin/bash

vDirIn=/IN/
vDirOut=/OUT/
vAction="Copy"

vExtn="avi,mkv,mp4"

vExtnArr=($(echo "$vExtn" | tr ',' 'n'))

for vExtnArr_i in "${!vExtnArr[@]}"
do
    echo "$vExtnArr_i: ${vExtnArr[vExtnArr_i]}"

    if [ "$vAction" == "Copy" ]
    then
       find "$vDirIn" -name '*.${vExtnArr[vExtnArr_i]' -exec cp -rf {} $vDirOut ;
    else
       find "$vDirIn" -name '*.${vExtnArr[vExtnArr_i]' -exec mv -f {} $vDirOut ;
    fi

done

here is the debug message, this is why I keep changing at the ‘find’ line. the output looks funny as I think

bash -x ./CopyMediaFiles.sh
+++ readlink -f ./CopyMediaFiles.sh
++ dirname '/CopyMediaFiles.sh'
+ vPGMDir='/'
+ date
+ vDirIn=/IN/
+ vDirOut=/OUT/
+ vAction=Copy
+ vExtn=avi,mkv,mp4
+ vExtnArr=($(echo "$vExtn" | tr ',' 'n'))
++ tr , 'n'
++ echo avi,mkv,mp4
+ for vExtnArr_i in '"${!vExtnArr[@]}"'
+ echo '0: avi'
0: avi
+ '[' Copy == Copy ']'
+ find /IN/ -name '*.${vExtnArr[vExtnArr_i]' -exec cp -rf '{}' /OUT/ ';'
+ for vExtnArr_i in '"${!vExtnArr[@]}"'
+ echo '1: mkv'
1: mkv
+ '[' Copy == Copy ']'
+ find /IN/ -name '*.${vExtnArr[vExtnArr_i]' -exec cp -rf '{}' /OUT/ ';'
+ for vExtnArr_i in '"${!vExtnArr[@]}"'
+ echo '2: mp4'
2: mp4
+ '[' Copy == Copy ']'
+ find /IN/ -name '*.${vExtnArr[vExtnArr_i]' -exec cp -rf '{}' /OUT/ ';'
+ for vExtnArr_i in '"${!vExtnArr[@]}"'

Advertisement

Answer

Are you missing a } from your find command?

-name '*.${vExtnArr[vExtnArr_i]' 

Should it be this?

-name "*.${vExtnArr[vExtnArr_i]}"
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement