I have a bash script that make symlinks of all files in Folder A to Folder B. It works all fines when it is all files, but it also has recursion where it calls the function to do the same for folders within Folder A.
The issue is that once it enters a folder in Folder A, the for-loop ends there as if it has reached the end of the array to loop.
Any ideas why is that? I already learned about need for the local variables and did try to change to them as best I could but for some reason I cannot keep the loop iterator?
function add_core_files() {
local FOLDER="${1}"
local LINK_TARGET="${2}"
printf "Copying symlinks from %s -> %s\n" "${FOLDER}" "${LINK_TARGET}"
cd "${FOLDER}" || ""
local FILES;
FILES=$(ls) # doing this to make the array local
printf "Found files (%s)\n" "${FILES}"
local FILE;
for FILE in ${FILES};
do
if [ "${FILE}" == ".." ] || [ "${FILE}" == "." ] || [ "${FILE}" == ".." ]; then
continue;
elif [ -L "${FILE}" ]; then
## symlink handling
# get the target dir of the symlink
local SYM_TARGET;
SYM_TARGET=$(cd "${FILE}"; pwd -P);
printf "Making symlink of symlink from %s -> %s\n" "${FOLDER}/${FILE}" "${LINK_TARGET}/${FILE}"
ln -s -f "${SYM_TARGET}" "${LINK_TARGET}/${FILE}"
elif [ -f "${FILE}" ]; then
printf "Making symlinks from %s -> %s\n" "${FOLDER}/${FILE}" "${LINK_TARGET}/${FILE}"
ln -s -f "${FOLDER}/${FILE}" "${LINK_TARGET}/${FILE}"
elif [ -d "${FILE}" ]; then
local TEMP_FOLDER="${FOLDER}/${FILE}";
local TEMP_TARGET="${LINK_TARGET}/${FILE}";
add_core_files "${TEMP_FOLDER}" "${TEMP_TARGET}"
printf "Back, FOLDER: %s LINK_TARGET: %s\n files: (%s)\n" "${FOLDER}" "${LINK_TARGET}" "${FILES}"
# <<<< ERROR HERE. Does not continue the for-loop
fi
done