検索のインポート:エラーオプション-o -name abc -o -name xyzは、* .properties検索を実行すると、検索からディレクトリセットを除外します。

検索のインポート:エラーオプション-o -name abc -o -name xyzは、* .properties検索を実行すると、検索からディレクトリセットを除外します。
#!/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コマンドラインから動的に追加するオプションが適切に区別されます。

次のスクリプトは、単一の検索パスであるターゲットディレクトリと無視するディレクトリ名を使用する非常に基本的なスクリプトです。このコードは、位置引数配列のfindオプションセットを作成します。$@

#!/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というディレクトリを無視して実行されますsomename。これにより、名前にスペース、タブ、および改行文字を含めることもできます(コマンドラインで引用した場合)。someothernamefluff

このコマンド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 ';' ')'

おすすめ記事