这是 Bash 函数的后续问题,它检查文件中是否有文本,如果没有则添加文本
我正在尝试创建一个 bash 函数来检查文件中是否包含文本。如果文本在文件中,则不要文本。如果是添加文本。我的代码是
#!/bin/bash
#Function that checks if text (ARGV1) is in a document (ARGV2). Please make ARGV1 an array of strings, with each new line a new entry in the array.
declare -a inputText=("[test]" "host=dynamic" "disallow=all" "allow=alaw" "allow=ulaw" "type=friend" "context=test" "secret=test")
function docCheckNReplace {
local text=$1
local document=$2
echo $document
local textLen=${#text[@]}
for ((i=0; i<textLen; i++)); do
if grep -q "${text[$i]}" $document; then
echo 'found'
echo ${test[$i]} 'was found in' $document
else
echo 'not found'
echo ${test[$i]} >> $document
fi
done
}
docCheckNReplace ${inputText[@]} /home/kam/Documents/TextingSed.txt
现在,每当我回显输入文件路径参数时,它都会返回“host=dynamic”。
当我将第一个参数设置为 inputText 而不是 ${inputText[@]} 它工作正常。
有人有什么想法吗?
谢谢 :)