qwert...@syberianoutpost.ru wrote:
> ~
> the thing is that I am trying to specify the directories to exclude and
> extensions to search for as input parameters (as files with one liners) not
> hardcoded in the script, but even though the variable echoes exactly what you
> coded find reports error: "No such file or directory"
> ~
> these are the script and the ouput:
> ~
> ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
> ~
> # directory separator
> _DIR_SEP="/"
>
> # ~ ~ ~ ~ ~ ~ ~ ~ reading files line by line ~ ~ ~ ~ ~ ~ ~ ~
> # save the field separator
> _IFS00=$IFS
> # end of line as field separator for file with one liners
> IFS=$'\n'
Remove those four lines -- use an array or $@.
>
> # search dir
> _SDIR="/media/sda3/tmp"
>
> # number of parameters passed to script
> _ARGSL=$#
>
> # debug
> echo "// __ number of parameters passed to script: "${_ARGSL}
>
> _FLS_XFLTR=" -type f \("
To use an array, replace that line with:
_FLS_XFLTR=( -type f )
>
> # Loop index
> _IxL=0
>
> while read X
> do
> echo "// __ ${_IxL} ${X}"
> if [ ${_IxL} -eq 0 ]; then
> _FLS_XFLTR="${_FLS_XFLTR} -name '${X}'"
> elif [ ${_IxL} -gt 0 ]; then
> _FLS_XFLTR="${_FLS_XFLTR} -or -name '${X}'"
> fi
> let _IxL=(${_IxL} + 1)
> done <$1
> _FLS_XFLTR="${_FLS_XFLTR} \) "
Replace from "while read X" to here with the following:
# The first time in the loop, $op will be '('. This will initialise
# the parenthesised list. Subsequently, $op will be '-o'.
op='('
while IFS= read X ; do
echo "// __ ${_IxL} ${X}"
_FLS_XFLTR+=( "$op" -name "$X" )
_IxL=$(( $_IxL + 1 ))
op='-o'
done < "$1"
if [ $_IxL -gt 0 ]; then
# If any input was read, terminate the parenthesised list.
_FLS_XFLTR+=( ')' )
fi
>
> echo "// __ \${_FLS_XFLTR}: |"${_FLS_XFLTR}"|"
echo "// __ \${_FLS_XFLTR}: |${_FLS_XFLTR[*]}|"
>
> find ${_SDIR} ${_FLS_XFLTR} -printf \
> '%T@,%A@,%C@,"%M",%n,"%l","%u","%g",%s,%d,'"${_SDIR}${_DIR_SEP}"'%P"\n'
Quote $_SDIR and replace ${_FLS_XFLTR}:
find "$_SDIR" "${FLS_XFLTR[@]}" -printf ...
>
> # restore default field separator
> IFS=$_IFS00
Not needed.
> ~
> ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
> ~
> // __ number of parameters passed to script: 1
> // __ 0 *.js
> // __ 1 *.txt
> // __ 2 *.gif
> // __ ${_FLS_XFLTR}: | -type f \( -name '*.js' -or -name '*.txt' -or -name
> '*.gif' \) | find: ` -type f \\( -name \'*.js\' -or -name \'*.txt\' -or -name
> \'*.gif\' \\) ': No such file or directory
Here's the loop to load the glob patterns from the file without arrays:
ifile=$1
set -- -type f
op='('
while IFS= read X ; do
set -- "$@" "$op" -name "$X"
_IxL=$(( $_IxL + 1 ))
op='-o'
done < "$ifile"
if [ $_IxL -gt 0 ]; then
set -- "$@" ')'
fi
The find command would then be modelled on:
find "$_SDIR" "$@" -printf ...