I have created the following code. At the end I want to process each file that is in the array individually but somewhere in this code I have not included the double quotes so the file names with a space in the middle it treats as 2 entries in the array:
#!/bin/bash
EXT=(sh mkv txt)
EXT_OPTS=()
# Now build the find command from the array
for i in "${EXT[@]}"; do
EXT_OPTS+=( -o -iname "*.$i" )
done
# remove the first thing in EXT_OPTS
EXT_OPTS=( "${EXT_OPTS[@]:1}" )
# Modify to add things to ignore:
EXT_OPTS=( '(' "${EXT_OPTS[@]}" ')' ! '(' -iname "*test*" -o -iname "*sample*" ')' )
#echo "${EXT_OPTS[@]}"
searchResults=($(find . -type f "${EXT_OPTS[@]}"))
#echo "$searchResults"
for R in "${searchResults[@]}"; do
echo "$R"
sleep 1
done
So the results I am getting are:
./Find2.sh
./untitled
2.sh
./countFiles.sh
./unrar.sh
./untitled
3.sh
./untitled
4.sh
./clearRAM.sh
./bash_test.sh
./Test_Log.txt
./untitled.txt
./Find.txt
./findTestscript.sh
./untitled.sh
./unrarTest.sh
./Test.sh
./Find.sh
./Test_Log
copy.txt
./untitled
5.sh
./IF2.sh
As an example untitled 5.sh has been added to the array as 2 entries. where have I forgot to add the "s
cheers
EDIT with suggested edits:
#!/bin/bash
EXT=(sh mkv txt)
EXT_OPTS=()
# Now build the find command from the array
for i in "${EXT[@]}"; do
EXT_OPTS+=( -o -iname "*.$i" )
done
# remove the first thing in EXT_OPTS
EXT_OPTS=( "${EXT_OPTS[@]:1}" )
# Modify to add things to ignore:
#EXT_OPTS=( "${EXT_OPTS[@]:-1}" )
EXT_OPTS=( '(' "${EXT_OPTS[@]}" ')' ! '(' -iname "*x0r*" -o -iname "*torrent*" ')' )
#echo "${EXT_OPTS[@]}"
#searchResults=($(find . -type f "${EXT_OPTS[@]}"))
#echo "$searchResults"
#for R in "${searchResults[@]}"; do
# echo "$R"
# sleep 1
#done
find . -type f "${EXT_OPTS[@]}" -exec sh -c '
for pathname do
printf "%s\n" "$pathname"
sleep 1
done' sh {} +
Now Produces:
./Find2.sh
./untitled 2.sh
./countFiles.sh
./unrar.sh
./untitled 3.sh
./Find3.sh
./untitled 4.sh
./clearRAM.sh
./bash_test.sh
./Test_Log.txt
./untitled.txt
./Find.txt
./findTestscript.sh
./untitled.sh
./unrarTest.sh
./Test.sh
./Find.sh
./Test_Log copy.txt
./untitled 5.sh
./IF2.sh
./Find4.sh