简体   繁体   中英

Concatenate a string with an array for recursively copy file in bash

I have a concatenation problem between a string and an array I want to copy all the files contained in the directories stored in the array, my command is in a loop (to recursively copy my files)

yes | cp -rf "./$WORK_DIR/${array[$i]}/"* $DEST_DIR

My array :

array=("My folder" "...") 

I have in my array several folder names (they have spaces in their names) that I would like append to my $WORK_DIR to make it possible to copy the files for cp . But I always have the following error

cp: impossible to evaluate './WORKDIR/my': No such files or folders
cp: impossible to evaluate 'folder/*': No such files or folders

This worked for me

#!/bin/bash

arr=("My folder" "This is a test")

i=0
while [[ ${i} -lt ${#arr[@]} ]]; do
    echo ${arr[${i}]}
    cp -rfv ./source/"${arr[${i}]}"/* ./dest/.
    (( i++ ))
done

exit 0

I ran the script. It gave me the following output:

My folder
'./source/My folder/blah-folder' -> './dest/./blah-folder'
'./source/My folder/foo-folder' -> './dest/./foo-folder'
This is a test
'./source/This is a test/blah-this' -> './dest/./blah-this'
'./source/This is a test/foo-this' -> './dest/./foo-this'

Not sure of the exact difference, but hopefully this will help.

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