获取查找:执行搜索 *.properties 时,错误选项 -o -name abc -o -name xyz 从搜索中排除目录集

获取查找:执行搜索 *.properties 时,错误选项 -o -name abc -o -name xyz 从搜索中排除目录集
#!/usr/bin/ksh

# *****************************************************************************************
# copy_properties.sh
# This script copies *.properties files from all directory excluding the 
# ones provided as the args. to a folder location of our choice.
# 
# *****************************************************************************************

echo "Starting the find and replace process for :" "$1"
set -x
# **************** Change Variables Here ************

startdirectory=$2 #"/home/ardsingh/test_properties/properties_files"
destinationFolder=$3

if [ -n "$4" ]; then
listOfFolderTobeIgnored=$4
#@list = split(/ /, $listOfFolderTobeIgnored);
#mapfile -t list << ($listOfFolderTobeIgnored)
IFS=' '
set -A list $listOfFolderTobeIgnored
foldersToBeIgnored="-o -name "${list[0]}
unset 'list[0]'
for item in "${list[@]}"
do
foldersToBeIgnored="$foldersToBeIgnored -o -name "
foldersToBeIgnored="$foldersToBeIgnored$item"
done
else
  echo "No input provided for folders to be ignored."
fi

#echo $foldersToBeIgnored

find "$startdirectory" -type d \( -name properties_file_folder_02_25 -o -name brmsdeploy -o -name TempJobs -o -name tmp -o -name logs -o -name deploy "$foldersToBeIgnored" \) -prune -o -name "*.properties" -type f -print -exec cp {} "$destinationFolder" \;

出现以下错误:

find: bad option -o -name abc -o -name xyz
find: [-H | -L] path-list predicate-list

答案1

而不是建立一个细绳对于忽略具有特定名称的目录的命令行选项,请使用数组。这样,find您从命令行动态添加的选项将被正确分隔。

以下脚本是一个非常基本的脚本,它采用单个搜索路径、目标目录,后跟任意数量的要忽略的目录名。该代码为位置参数数组findin构建了一组选项。$@

#!/usr/bin/ksh

searchpath=$1
destdir=$2
shift 2

ignore=( brmsdeploy TempJobs tmp logs deploy )

if [ ! -d "$destdir" ]; then
    printf 'Destination directory "%s" does not exist\n' >&2
    exit 1
fi

# Process the directories to ignore from the command line.
# This replaces the positional parameters.
for name do
    set -- "$@" -o -name "$name"
    shift
done

# Process the directories that we always ignore.
# This adds to the positional parameters.
for name in "${ignore[@]}"; do
    set -- "$@" -o -name "$name"
done

shift # shift off the first -o

find "$searchpath" \( -type d \( "$@" \) -prune \) -o \
    \( -type f -name '*.properties' -exec cp {} "$destdir" \; \)

使用方法:

./script.sh /path/to/topdir /path/to/destdir somename someothername fluff

这将执行find,忽略名称为somenamesomeothername和 的 目录fluff。请注意,这还允许这些名称包含空格、制表符和换行符(如果它们在命令行上被引用)。

该命令实际find执行的命令是

find /path/to/topdir '(' -type d '(' -name somename -o -name someothername -o -name fluff -o -name brmsdeploy -o -name TempJobs -o -name tmp -o -name logs -o -name deploy ')' -prune ')' -o '(' -type f -name '*.properties' -exec cp '{}' /path/to/destdir ';' ')'

相关内容