in reply to Defactor this code
For things like this, it's nice to be familiar with some of the more esoteric shortcuts perl offers. I typically do something like that with a one-liner, probably something like:
Although given that the order of directives in the sshd_config file doesn't matter, that could just beperl -i.old -lne 'if (/^[\s#]*Port/i) { print "Port 1234" unless $done +++ } else { print }' sshd_config
I don't see a whole lot of point in prompting for the port number, but if you want it separated out of the script you could useperl -i.old -lne 'print unless /^[\s#]*Port/i; END{print "Port 1234"}' + sshd_config
orPORT=1234 perl -i.old -lne 'print unless /^[\s#]*Port/i; END { print " +Port $ENV{PORT}" }' sshd_config
All of these are untested, sorry.perl -i.old -lne 'BEGIN { $sshport = pop }; print unless /^[\s#]*Port/ +i; END { print "Port $sshport" }' sshd_config 1234
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Defactor this code
by smahesh (Pilgrim) on Feb 11, 2007 at 07:34 UTC |
In Section
Seekers of Perl Wisdom