[CentOS] parsing /proc/cmdline

Tue Mar 11 03:49:43 UTC 2008
Dag Wieers <dag at centos.org>

On Mon, 10 Mar 2008, Jim Wight wrote:

> On Fri, 2008-03-07 at 11:59 -0500, Ross S. W. Walker wrote:
>> Ross S. W. Walker wrote:
>>> Jerry Geis wrote:
>>>>
>>>> Hi - I am not an expert at shell script writing.
>>>> If /proc/cmdline looks like
>>>>
>>>> option1 option2 ... ks=http://192.168.1.8/ks/ks.cfg option3
>>>> option 4 ...
>>>>
>>>> How can I get the 192.168.1.8 out of this cmdline.
>>>
>>> Try:
>>>
>>> # IPADDR=`cat /proc/cmdline | sed 's/.*\/\([1-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)\/.*/\1/'`
>>>
>>> This will find an IP in between /.../
>>
>> Actually shorter sed line:
>>
>> # IPADDR=`cat /proc/cmdline | sed 's/.*\/\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)\/.*/\1/'`
>
> And shorter still:
>
>   sed 's/.*\/\(\([0-9]\+\.\)\{3,\}[0-9]\+\).*/\1/'
>
> which uses \{3,\} to specify the 3 occurrences of [0-9]\+\.
>
> However, I would simply go for something like:
>
>   sed 's,.*http://\(.*\)/ks/.*,\1,'

How about:

     sed 's|.*\bks=\w\+://\([^/]\+\)/.*|\1|'

that would work with nfs, ftp or https as well and would not falsly match 
another URL in the cmdline. Plus it does not require the hostname to be an 
IP address.

-- 
--   dag wieers,  dag at centos.org,  http://dag.wieers.com/   --
[Any errors in spelling, tact or fact are transmission errors]