[NTLUG:Discuss] sysV script help
Chris Cox
cjcox at acm.org
Thu Sep 25 15:17:30 CDT 2003
m m wrote:
> Chris:
>
> Thanks for the information. I think I want to know the information about
> write the script.
> or some one give me a piece of example code?
Easiest if we can use some perl.
>
> the code I need can do:
>
> find current ip (i am using dhcp, it changes sometimes)
myip=`ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | cut -f2 -d:`
> change the new ip on all text files in a directory
(now the find will find all files in the current directory
and under....)
find <dir> -type f -exec file | grep -i text | cut -f1 -d: |
xargs perl -pi -e "s/<oldip>/$myip/g"
Might not handle filenames with spaces.
Might have to replace <oldip> with something generic in case
you don't have the prior value e.g.:
[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*
(which maches anthing like 1.2.3.4)
(actually you can probably use extended REs with perl... so
you can reduce this a bit to look for 1 or more occurence
of the digits instead of looking for a digit followed by
0 or more occurences)
If you just want to process files in the current dir (and
not recurse under them):
file * | grep -i text | cut -f1 -d: |
xargs perl -pi -e "s/<oldip>/$myip/g"
> change text on the 3rd line on all files in a directory
Same pipeline as before, but change the perl to:
perl -pi -e 'if ($. == 3) { s/fred/george/;} '
Somebody may have a better solution...
>
> thanks in advance.
>
>> From: Chris Cox <cjcox at acm.org>
>> Reply-To: NTLUG Discussion List <discuss at ntlug.org>
>> To: NTLUG Discussion List <discuss at ntlug.org>
>> Subject: Re: [NTLUG:Discuss] sysV script help
>> Date: Thu, 18 Sep 2003 09:55:33 -0500
>>
>> m m wrote:
>>
>>> Hi all:
>>>
>>> is anyone can point me direction where are the sysV tutorial?
>>>
>>> basically what I need is when a system starts, it will run this
>>> script automatically
>>>
>>> find current ip (i am using dhcp, it changes sometimes)
>>> change the new ip on all text files in a directory
>>> change text on the 3rd line on all files in a directory
>>>
>>
>> If you google for it, you'll come up with two primary sources
>> of info... Redhat's docs (ok, but reliant upon their chkconfig
>> tool.. which IS NOT standard sysV init... of course, if you
>> are using Redhat, then the doc may be just the thing for you),
>> the other source is Linux From Scratch, but it really doesn't
>> describe it very well.
>>
>> I did find this note out of the Solaris Managers discussion
>> list, which I think does an adequate job. However, please
>> note that Solaris does something absolutely EVIL in that
>> their runlevel 3 depends upon runlevel 2 (runlevel 2 scripts
>> get executed as a part of going into runlevel 3... ICK!!!).
>>
>> http://www.sunmanagers.org/pipermail/summaries/2000-December/000086.html
>>
>> Some of the commercial Linux implementations of SysV init are
>> clearly on steroids and do some very elegant things. Both Redhat
>> and SuSE have a stock preamble comment format (both different however,
>> yet both can coexist) which allow their favorite admin utilities,
>> chkconfig on RedHat, and YaST RunLevel Editor on SuSE to automatically
>> detect new scripts and does the "linking" into the right directories
>> for you as directed by those utilities. Of course, you won't
>> really learn SysV init doing it that way.... always good to understand
>> what is really going on... then when you're comfortable, you can
>> use the distribution admin utilities (I know that seems backwards,
>> but this the better approach for a future Sys Admin vs. a casual
>> user).
>>
>>
>>
>>
>>
>> _______________________________________________
>> https://ntlug.org/mailman/listinfo/discuss
>
>
> _________________________________________________________________
> Tired of spam? Get advanced junk mail protection with MSN 8.
> http://join.msn.com/?page=features/junkmail
>
>
> _______________________________________________
> https://ntlug.org/mailman/listinfo/discuss
>
>
More information about the Discuss
mailing list