当前位置: 动力学知识库 > 问答 > 编程问答 >

How to retrieve a captured group regex in busybox sed

问题描述:

I'm making a script to boot a TS-7400 ARM SBC and I want it to be able to read some arguments and optional kernel parameters passed via a configuration file found on a SDcard. I called my config file syscfg.conf and it is organized using KEYWORD=value pairs, but since kernel arguments themselves can have the same syntax, I thought of delimiting values like this:

CMDLINE_ARGS="elevator=noop scheduler=noop"

While testing in regular bash, I was able to isolate the kernel command line arguments using either one of these methods:

$ grep CMDLINE_ARGS syscfg.conf | sed 's/CMDLINE_ARGS="\(.*\)"/\1/'

elevator=noop scheduler=noop

$ grep CMDLINE_ARGS syscfg.conf | cut -d'"' -f2

elevator=noop scheduler=noop

$ awk -F'"' '/CMDLINE_ARGS/ {print $2}' syscfg.conf

elevator=noop scheduler=noop

but when it runs on TS-LINUX, which is a busybox-based stripped down Linux used to boot a custom kernel or application, it doesn't work like in regular bash. While the awk command doesn't even exist, the cut version worked fine, but the sed one returns this:

CMDLINE_ARGS="elevator=noop scheduler=noop"

Why is busybox's sed implementation behaving like this? Instead of returning the whole string, I expected it to output only the "\1" group of any characters between the " delimiters - the "(.*)" regex. Is there any way we can make it work like in bash?

网友答案:
sed '/CMDLINE_ARGS=/ {s/CMDLINE_ARGS=.//;s/.$//;}' syscfg.conf

try maybe this if quote are problem or the pipe (i suspect shell substitution in one of those part)

分享给朋友:
您可能感兴趣的文章:
随机阅读: