簡體   English   中英

Linux Bash - 排除帶有find的目錄

[英]Linux Bash - excluding directories with find

我正在編寫一個正在編寫的bash腳本時出現問題,並且我已經隔離了有問題的代碼。

從變量使用參數時查找命令不排除目錄。

現在我需要將'-not -path ./dir'參數分配給變量,因為它正在循環,但是如果我這樣做,它似乎不起作用。

我已經在下面包含了生成所需輸出的命令,以及我當前打破的版本。 請看下面的測試:

變量賦值

findIgnorePaths="-not -path ./\.git\* -not -path ./administrator/cache\* -not -path ./images\* -not -path ./logs\* -not -path ./cache\* -not -path ./media\* -not -path ./plugins\* -not -path ./tmp\*"

期望的輸出。

find ./ -type d -not -path ./\.git\* -not -path ./administrator/cache\* -not -path ./images\* -not -path ./logs\* -not -path ./cache\* -not -path ./media\* -not -path ./plugins\* -not -path ./tmp\*

輸出破損

find ./ -type d ${findIgnorePaths}

對於任何想要查看我正在處理上述問題的腳本的人,請看下面的內容:

原始劇本(不需要閱讀以回答問題,但......為那些好奇的靈魂提供)

#!/bin/sh

declare -a ignoreDirs=()
findIgnorePaths=""
seperator="\e[33m**********************************\e[0m\n"

clear
printf "\n\n"

if [[ "${PWD##*/}" != "public_html" ]]
then
    printf "\e[31mThis script will not run unless current location is; /home/*/public_html/*\e[0m\n"
    exit
fi

echo "Site Locker! This will change all directory permissions to 555. And all file permissions to 444. Read only accces."

echo #Move to new line
echo "Are you sure you want to make changes to files/directories within the below location?"
printf "$seperator"
printf "\e[0;49;96m${PWD##/home/}\e[0m\n"
printf "$seperator"
echo #Move to new line

read -r -p "Y/n " response
echo #Move to new line
if [[ $response =~ ^([nN][oO]|[nN])$ ]]
then
    printf "\n\e[31mNo changes made. Quitting.\e[0m\n\n"
    exit
fi

printf "\e[95mIf you're working with a Joomla site, please select\nthe **JOOMLA** preset in the list below.\e[0m\n\nPlease select directory #) to add to the ignore list:\n\e[92m"

currentDir=( "**SAVE/SKIP**" "**JOOMLA**" )
currentDir+=( $(find . -maxdepth 1 -type d) )
select DIR in ${currentDir[@]};
do
  case $DIR in
        *SAVE*)
          printf "\n\n\e[92mDone!!\e[0m\n\n"
          break
          ;;
        *JOOMLA*)
          printf "\n\e[92mApplying Joomla preset.\n"
          ignoreDirs+=("./.git" "./administrator/cache" "./cache" "./images" "./logs" "./media" "./tmp" "./plugins")
          findIgnorePaths+=" -not -path ./\.git\* -not -path ./administrator/cache\* -not -path ./images\* -not -path ./logs\* -not -path ./cache\* -not -path ./media\* -not -path ./plugins\* -not -path ./tmp\*"
          printf "\n\n\e[31mIgnore list:\n"
          for item in "${ignoreDirs[@]}"
          do
              printf "$item\n"
          done
          ;;
        *)
          ignoreDirs+=("$DIR")
          findIgnorePaths+=" -not -path ${DIR}\*"
          printf "\n\n\e[31mIgnore list:\n"
          for item in "${ignoreDirs[@]}"
          do
              printf "$item\n"
          done
          printf "\e[92m"
          ;;
  esac
done
findIgnorePaths=$(echo ${findIgnorePaths} | cut -c 1-)

printf "\e[0m\n"

echo #Move to new line
printf "$seperator"
echo #Move to new line

echo "Apply 555 permissions to the below directories & all sub directories? "
printf "$seperator"
printf "\e[0;49;96m"

echo
echo "$findIgnorePaths"
echo
find ./ -maxdepth 1 -type d -not -path ./\.git\* -not -path ./administrator/cache\* -not -path ./images\* -not -path ./logs\* -not -path ./cache\* -not -path ./media\* -not -path ./plugins\* -not -path ./tmp\*
echo
find ./ -maxdepth 1 -type d ${findIgnorePaths}
echo



printf "\e[0m\n"
printf "$seperator"

read -r -p "Y/n " response
echo #Move to new line
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
    find ./ -type d $findIgnorePaths -exec chmod 555 {} +
    printf "\e[92mChanged directory permissions to 555\e[0m\n\n"
else
    printf "\e[31mSkipping this step. No directory permissions were set.\e[0m\n\n"
fi

read -r -p "Apply 444 permissions to all files in; ${PWD##*/}? Y/n " response
echo #Move to new line
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
    find ./ -type f $findIgnorePaths -exec chmod 444 {} +
    printf "\e[92mChanged file permissions to 444\e[0m\n\n"
else
    printf "\e[31mSkipping this step. No file permissions were set.\e[0m\n\n"
fi

printf "\n\e[92mFinished!\e[0m\n\n"

這是Bash FAQ 50 ,它建議您使用數組變量將參數傳遞給程序:

findIgnorePaths=(-not -path './.git*' -not -path './administrator/cache*' -not -path './images*' -not -path './logs*' -not -path './cache*' -not -path './media*' -not -path './plugins*' -not -path './tmp*')
find ./ -type d "${findIgnorePaths[@]}"

注意目錄globs是單引號,因此在創建變量時shell不會解釋它們。 你的反斜杠逃脫做了同樣的事情; 這是個人偏好的問題,你覺得哪一個更容易閱讀。

通過shellcheck.net運行腳本總是一個好主意,找到可能潛伏的任何問題。

另外, /bin/sh不是/bin/bash ,所以修復你的shebang!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM