#==============================================================================#
helper.sh (developed from the original /usr/sbin/helper.sh)
#==============================================================================#
#!/bin/sh
################################################################################
# #
# Asuswrt-Merlin helper functions. For use with Postconf scripts (and others) #
# #
################################################################################
_quote() { echo $1 | sed 's/[]\/()$*.^|[]/\\&/g'; }
_assert_var_is_set() { eval [ \${$1+X} ]; }
_assert_var_is_not_set() { eval [ -z \${$1+X} ]; }
_assert_var_is_empty() { eval [ -z \${$1:+X} ]; }
_assert_var_is_not_empty() { eval [ \${$1:+X} ]; }
_assert_var_is_equal_to() { eval [ \"\$$1\" == \"\$2\" ]; }
_assert_var_is_not_equal_to() { eval [ \"\$$1\" != \"\$2\" ]; }
_random_string() { echo $(</dev/urandom tr -cd 'a-zA-Z' | head -c $1); }
################################################################################
# #
# insert_line PATTERN NEWLINE FILE #
# #
################################################################################
# #
# This function looks for a string (first argument), and inserts a specified #
# string (second argument) into a new line (after each matching line), inside #
# a given file (third argument). #
# #
# This function will modify the given file only if the INLINE global/external #
# variable is undefined or set to 1. In all other cases i.e. when it is #
# defined but empty or set to any value different from 1, the output of the #
# function is sent to the standard output (useful to create new files). #
# #
# This function will treat the first argument as a regular expression/pattern #
# only if the REGEX global/external variable is set to 1. In all other cases #
# i.e. when it is undefined, defined but empty or set to any value different #
# from 1, sed will not treat the first argument as a regular expression. #
# #
# PATTERN: the line to locate #
# NEWLINE: the line to insert #
# FILE: file where to insert #
# #
# USAGE: /jffs/scripts/helper.txt #
# #
################################################################################
insert_line() {
local PATTERN=$(_quote "$1")
local CONTENT=$(_quote "$2")
local FILE=$(readlink -f $3)
local OPTS=
_assert_var_is_equal_to REGEX 1 && PATTERN=$1
_assert_var_is_not_set INLINE || _assert_var_is_equal_to INLINE 1 && OPTS='-i'
sed $OPTS "/$PATTERN/a$CONTENT" $FILE
}
################################################################################
# #
# remove_line PATTERN FILE #
# #
################################################################################
# #
# This function looks for a string (first argument) and removes the #
# corresponding line inside a given file (second argument). #
# #
# This function will modify the given file only if the INLINE global/external #
# variable is undefined or set to 1. In all other cases i.e. when it is #
# defined but empty or set to any value different from 1, the output of the #
# function is sent to the standard output (useful to create new files). #
# #
# This function will treat the first argument as a regular expression/pattern #
# only if the REGEX global/external variable is set to 1. In all other cases #
# i.e. when it is undefined, defined but empty or set to any value different #
# from 1, sed will not treat the first argument as a regular expression. #
# #
# PATTERN: the line to locate #
# FILE: file where to insert #
# #
# USAGE: /jffs/scripts/helper.txt #
# #
################################################################################
remove_line() {
local PATTERN=$(_quote "$1")
local FILE=$(readlink -f $2)
local OPTS=
_assert_var_is_equal_to REGEX 1 && PATTERN=$1
_assert_var_is_not_set INLINE || _assert_var_is_equal_to INLINE 1 && OPTS='-i'
sed $OPTS "/$PATTERN/d" $FILE
}