Sorry, I missed one patch and have resent v2. Please ignore this patch
set.
On Wed, Feb 16, 2022 at 01:23:17PM +0800, Coiby Xu wrote:
Use sed command group and conditional control to get rid of grep.
Suggested-by: Philipp Rudo <prudo(a)redhat.com>
Signed-off-by: Coiby Xu <coxu(a)redhat.com>
---
kdumpctl | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/kdumpctl b/kdumpctl
index 9fd76ac..998ac6e 100755
--- a/kdumpctl
+++ b/kdumpctl
@@ -1405,19 +1405,22 @@ GRUB_ETC_DEFAULT="/etc/default/grub"
# $2: new value. If empty, the parameter would be removed
_update_kernel_cmdline_in_grub_etc_default()
{
- local _para=$1 _val=$2 _para_val _regex
+ local _para=$1 _val=$2 _para_val
if [[ -n $_val ]]; then
_para_val="$_para=$_val"
fi
- _regex='^(GRUB_CMDLINE_LINUX=.*)([[:space:]"])'"$_para"'=[^[:space:]"]*(.*)$'
- if grep -q -E "$_regex" "$GRUB_ETC_DEFAULT"; then
- sed -i -E
's/'"$_regex"'/\1\2'"$_para_val"'\3/'
"$GRUB_ETC_DEFAULT"
- elif [[ -n $_para_val ]]; then
- # If the kernel parameter doesn't exist, put it in the first
- sed -i -E 's/^(GRUB_CMDLINE_LINUX=")/\1'"$_para_val"' /'
"$GRUB_ETC_DEFAULT"
- fi
+ # If the parameter already exists, replace it with new value.
+ # Otherwise insert it to the end.
+ sed -i -E "/^GRUB_CMDLINE_LINUX=/ {
+
s/([[:space:]\"])crashkernel=[^[:space:]\"]*(.*)$/\1${_para_val}\2/;
+ # t: jump to a label only if a s/// command has succeeded.
+ # Since there is no label, it would jump to the end i.e.
+ # skip the last command if the parameter already exist
+ t;
+ s/\"$/ ${_para_val}\"/
+ }" "$GRUB_ETC_DEFAULT"
}
reset_crashkernel()
--
2.34.1
--
Best regards,
Coiby