Updates
This commit is contained in:
@@ -160,3 +160,73 @@ _isKVM() {
|
||||
echo 1
|
||||
fi
|
||||
}
|
||||
|
||||
# _replaceInFile $startMarket $endMarker $customtext $targetFile
|
||||
_replaceInFile() {
|
||||
|
||||
# Set function parameters
|
||||
start_string=$1
|
||||
end_string=$2
|
||||
new_string="$3"
|
||||
file_path="$4"
|
||||
|
||||
# Counters
|
||||
start_line_counter=0
|
||||
end_line_counter=0
|
||||
start_found=0
|
||||
end_found=0
|
||||
|
||||
if [ -f $file_path ] ;then
|
||||
|
||||
# Detect Start String
|
||||
while read -r line
|
||||
do
|
||||
((start_line_counter++))
|
||||
if [[ $line = *$start_string* ]]; then
|
||||
# echo "Start found in $start_line_counter"
|
||||
start_found=$start_line_counter
|
||||
break
|
||||
fi
|
||||
done < "$file_path"
|
||||
|
||||
# Detect End String
|
||||
while read -r line
|
||||
do
|
||||
((end_line_counter++))
|
||||
if [[ $line = *$end_string* ]]; then
|
||||
# echo "End found in $end_line_counter"
|
||||
end_found=$end_line_counter
|
||||
break
|
||||
fi
|
||||
done < "$file_path"
|
||||
|
||||
# Check that deliminters exists
|
||||
if [[ "$start_found" == "0" ]] ;then
|
||||
echo "ERROR: Start deliminter not found."
|
||||
sleep 2
|
||||
fi
|
||||
if [[ "$end_found" == "0" ]] ;then
|
||||
echo "ERROR: End deliminter not found."
|
||||
sleep 2
|
||||
fi
|
||||
|
||||
# Replace text between delimiters
|
||||
if [[ ! "$start_found" == "0" ]] && [[ ! "$end_found" == "0" ]] && [ "$start_found" -le "$end_found" ] ;then
|
||||
# Remove the old line
|
||||
((start_found++))
|
||||
|
||||
if [ ! "$start_found" == "$end_found" ] ;then
|
||||
((end_found--))
|
||||
sed -i "$start_found,$end_found d" $file_path
|
||||
fi
|
||||
# Add the new line
|
||||
sed -i "$start_found i $new_string" $file_path
|
||||
else
|
||||
echo "ERROR: Delimiters syntax."
|
||||
sleep 2
|
||||
fi
|
||||
else
|
||||
echo "ERROR: Target file not found."
|
||||
sleep 2
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user