#!/usr/bin/perl use strict; use warnings; my $sshport = 0; until ($sshport) { print "Please enter SSH port number> "; chomp($sshport = ); $sshport = 0 unless $sshport =~ /^[1-9]\d{1,9}$/; } { local $^I = ".old"; # the "in-place edit backup extension" variable local @ARGV = ("sshd_config"); # the files to edit in-place while (<>) { s/^(#?)port .*/$1Port $sshport/i; # this KEEPS the '#' character, # which I think is the correct behavior } }