I have been struggling for a while with the folllowing problems I have written scripts in which any command run is first put into a string, and then executed. That way, the command is written only once and it can be analysed, displayed, used several times. It works fine most of the time but not in the following detailed cases below. So if any of you can help, I would be more then happy ;) I must admit I have always struggle to understand the way script works when dealing with variables containing quotes and spaces, so if you know of a good tutorial ...
Case 1: mkdir command
dir="/tmp/Mytests/DirWith2 spaces 2"
cmdToRun="mkdir -p ${dir}"
echo "Running [${cmdToRun}]"
${cmdToRun} # <= this does not work, OK it seems normal !
cmdToRun="mkdir -p """${dir}""""
${cmdToRun} # <= this does not work either !
cmdToRun="mkdir -p \"${dir}\""
${cmdToRun} # <= this does not work either !
Case 2: rsync command
rsync_cmd="rsync -avz --stats "
rsync_destinationBaseDir="/tmp/ToTestRsync"
ListOfDirToSynchronize=( \
"/opt/dir1/subdir1" "/opt/dir1space 1/subdir1" "/opt/dir1space 1/subdir1space 1" )
rsync_host_from="root@SRV1:"
A loop on the directories in my ListOfDirToSynchronize
{
SourceDir="${ListOfDirToSynchronize[$i]}"
( # here we start a subshell to run // rsyncsto speed-up the whole process
cmdToRun="${rsync_cmd} ${rsync_host_from}'${SourceDir}/' ${rsync_destinationBaseDir}${SourceDir}"
funcLog " | INFO | Directories synchronisation | SubprocessID [${subProcessID}] | running command [${cmdToRun}]"
${cmdToRun} >> ${LOG_FILE} 2>&1 #<== this does not work when spaces in the directories
) # end of subshell
} # end of the loop
