From: Roy Marples Date: Fri, 15 Aug 2008 09:07:56 +0000 (+0000) Subject: sed may not always be available, so use shell loops to replicate behaviour in this... X-Git-Tag: v4.0.2~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7bcaeef3a2cba652e43d4c3459240fb333875637;p=thirdparty%2Fdhcpcd.git sed may not always be available, so use shell loops to replicate behaviour in this case --- diff --git a/dhcpcd-hooks/20-resolv.conf b/dhcpcd-hooks/20-resolv.conf index 7304f696..8a3deab8 100644 --- a/dhcpcd-hooks/20-resolv.conf +++ b/dhcpcd-hooks/20-resolv.conf @@ -24,18 +24,16 @@ build_resolv_conf() done # Build the search list - search=$(cd "${resolv_conf_dir}"; - sed -n 's/^search //p' ${interfaces}) + search=$(cd "${resolv_conf_dir}"; \ + key_get_value "search" ${interfaces}) [ -n "${search}" ] && search="search $(uniqify ${search})\n" # Build the nameserver list - srvs=$(cd "${resolv_conf_dir}"; - sed -n 's/^nameserver //p' ${interfaces}) - if [ -n "${srvs}" ]; then - for x in $(uniqify ${srvs}); do - servers="${servers}nameserver ${x}\n" - done - fi + srvs=$(cd "${resolv_conf_dir}"; \ + key_get_value "nameserver" ${interfaces}) + for x in $(uniqify ${srvs}); do + servers="${servers}nameserver ${x}\n" + done fi header="${signature_base}${header:+ ${from} }${header}" diff --git a/dhcpcd-hooks/50-ntp.conf b/dhcpcd-hooks/50-ntp.conf index 40554fc7..6cf6a2dd 100644 --- a/dhcpcd-hooks/50-ntp.conf +++ b/dhcpcd-hooks/50-ntp.conf @@ -30,7 +30,7 @@ build_ntp_conf() # Build a server list srvs=$(cd "${ntp_conf_dir}"; - sed -n 's/^server //p' ${interfaces}) + key_get_value "server" ${interfaces}) if [ -n "${srvs}" ]; then for x in $(uniqify ${srvs}); do servers="${servers}server ${x}\n" @@ -40,7 +40,7 @@ build_ntp_conf() # Merge our config into ntp.conf [ -e "${cf}" ] && rm -f "${cf}" - sed "/^${signature_base}/,/^${signature_base_end}/d" \ + remove_markers "${signature_base}" "${signature_base_end}" \ /etc/ntp.conf > "${cf}" if [ -n "${servers}" ]; then echo "${signature_base}${header:+ ${from} }${header}" >> "${cf}" diff --git a/dhcpcd-run-hooks.in b/dhcpcd-run-hooks.in index 47e6316d..2bb84bab 100644 --- a/dhcpcd-run-hooks.in +++ b/dhcpcd-run-hooks.in @@ -36,6 +36,48 @@ list_interfaces() echo "${interfaces}" } +# We normally use sed to extract values using a key from a list of files +# but sed may not always be available at the time. +key_get_value() +{ + local key="$1" value= x= line= + + shift + if type sed >/dev/null 2>&1; then + sed -n "s/^${key} //p" $@ + else + for x; do + while read line; do + case "${line}" in + "${key} "*) echo "${line##${key} }";; + esac + done < "${x}" + done + fi +} + +# We normally use sed to remove markers from a configuration file +# but sed may not always be available at the time. +remove_markers() +{ + local m1="$1" m2="$2" x= line= in_marker=0 + + shift; shift + if type sed >/dev/null 2>&1; then + sed "/^${m1}/,/^${m2}/d" $@ + else + for x; do + while read line; do + case "${line}" in + "${m1}"*) in_marker=1;; + "${m2}"*) in_marker=0;; + *) [ ${in_marker} = 0 ] && echo "${line}";; + esac + done < "${x}" + done + fi +} + # Compare two files # It different, replace first with second otherwise remove second change_file()