我正在撰寫一個 shell 腳本,但遇到了需要將引數傳遞給函式的問題。但它目前沒有按預期作業。我請求你仔細閱讀劇本,讓我知道我在這里遺漏了什么。
如果我直接在腳本檔案(test.sh)中撰寫以下代碼而沒有任何功能,那么它作業正常。
source ./logger.sh
now=$(date "%Y.%m.%d_%H.%M.%S")
logFile="/path/to/the/directory/${now}.log"
touch $logFile
scriptName="test.sh"
source="/path/to/the/directory"
older=7
while IFS= read -r -d $'\0'; do
printf "\nIn while: Currently processing file: ${REPLY}\n"
file=$REPLY
log 1 $scriptName "main" "Currently processing file: ${file}" $logFile
done < <(find ${source} -maxdepth 1 -type f -mtime ${older} ! -name '*.FLAG' ! -name '*.FAIL' -print0)
檔案中的輸出
2021-Oct-24 09:37:12 test.sh: main: Currently processing file: /path/to/the/directory/file1.txt
2021-Oct-24 09:37:12 test.sh: main: Currently processing file: /path/to/the/directory/file2.txt
..... more files ...
但是,當我將腳本封裝到函式呼叫中時,它無法正常作業。
function moveFiles() {
source=$1
older=$2
logFile=$3
log 1 $scriptName "moveFiles" "Finding files from ${source} which changed before ${older} days." $logFile
while IFS= read -r -d $'\0'; do
printf "\nIn while: Currently processing file: ${REPLY}\n"
file=$REPLY
log 1 $scriptName "moveFiles" "Currently processing file: ${file}" $logFile
done < <(find ${source} -maxdepth 1 -type f -mtime ${older} ! -name '*.FLAG' ! -name '*.FAIL' -print0)
log 1 $scriptName "moveFiles" "For loop completed" $logFile
}
source ./logger.sh
now=$(date "%Y.%m.%d_%H.%M.%S")
logFile="/path/to/the/directory/${now}.log"
touch $logFile
scriptName="test.sh"
moveFiles "/path/to/the/directory" 7 $logFile
log 2 $scriptName "main" "Move files - completed" $logFile
檔案中的輸出
2021-Oct-24 09:43:23 test.sh: moveFiles: Finding files from /path/to/the/directory which changed before 7 days.
2021-Oct-24 09:43:23 test.sh: moveFiles: For loop completed
當我在沒有函式的情況下直接運行腳本時得到的輸出,我也需要通過函式獲得相同的輸出。請幫我找出問題所在。
Also I would like to prefer looping an array of files instead of providing find command output to while loop. I could not find any valid solution, that is why I opted using this approach. If there is any better approach, I would definitely like to opt that.
Note: The log function is in the logger.sh which I included via source ./logger.sh and it seems to be working fine.
EDIT 1: I am looking for a way to store the find command output to array and then send the array to another function and use loop there to process the elements of the array. Is there a better way that I can do? I tried below but it is not working. I always get only 1 element in the array.
list=$(find ${source} -maxdepth 1 -type f -mtime ${older} ! -name '*.FLAG' ! -name '*.FAIL' -print0)
processInLoop ${list[@]}
In the other function
function processInLoop(){
fileList=("${@}")
}
Thank you in advance.
uj5u.com熱心網友回復:
在下面嘗試使用您的目錄和 find 陳述句中的過濾器。
function moveFiles() {
source=$1
older=$2
logFile=$3
echo 1 $scriptName "moveFiles" "Finding files from ${source} which changed before ${older} days." $logFile
find ${source} -maxdepth 1 -type f -mtime 1 -name '*' -print0 |
while IFS= read -r -d ''; do
echo "\nIn while: Currently processing file: ${REPLY}\n"
file=$REPLY
echo $scriptName "moveFiles" "Currently processing file: ${file}"
$logFile
done
echo $scriptName "moveFiles" "For loop completed" $logFile
}
now=$(date "%Y.%m.%d_%H.%M.%S")
logFile="."
touch $logFile
scriptName="test.sh"
moveFiles "." 7 $logFile
echo $scriptName "main" "Move files - completed" $logFile
uj5u.com熱心網友回復:
我正在尋找一種將 find 命令輸出存盤到陣列的方法...
對于那個用途readarray。
readarray -d '' -t arr < <(find ....)
yourfunction "${arr[@]}"
腳本有很多問題 - 不帶引號的變數擴展,使用普通變數作為陣列,試圖在字串中存盤零位元組。使用 shellcheck 檢查您的腳本。
參考變數擴展以防止分詞和檔案名擴展。不要touch $logFile,做touch "$logFile"。
$'\0'與 相同''。每個字串以零位元組結尾,兩個零位元組與一個零位元組相同,因為字串是解釋器直到第一個零位元組。
主觀:我不喜歡pascal case,我更喜歡snake case。
list=$(... -print0)- 因為 strngs 最多存盤零位元組,所以不能在變數中存盤零位元組。您可以通過管道將其xxd -p轉換為十六進制,或如上所示將其讀取到陣列。你應該從 bash 那里得到一個警告。
list=$(...) ; func ${list[@]}- 變數擴展沒有被參考,所以它經歷了分詞和檔案名擴展。list不是陣列,所以它只有一個元素。list=$( ... )分配一個變數,list=( ... )將分配一個陣列。
主觀:find ... ! ...- 雖然在非互動式腳本中禁用了歷史擴展,但我建議!無論如何都要參考,這樣當您將內容復制到終端以“測驗它”時,不會觸發歷史擴展。我會做find ... '!' ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/341832.html
下一篇:解密檔案gpg時添加進度條
