I’m currently writing a Perl script to parse a config file. The syntax is as follows
{command parameter1 parameter2}
where the second parameter is optional. In the first place I just want to extract the content between { and }. Im using this code
while (<FILE>) {
chomp;
unless ($_ =~ m/^\/\//) {
$_ =~ /^\{(.*?)\}/s;
print $1;
}
Instead of the print command the string will be evaluated further. My problem now is that the script just stops working with some strings
The script works, as long there are quotation marks around the arguments. This
{exec sed 's/ClientAliveInterval\ 300/ClientAliveInterval\ 1800/\' /etc/ssh/sshd_config > /etc/ssh/sshd_config.new}
will return the content between the brackets, but since there could be a second parameter it is now hard to distinguish the parameters without qoutation marks
Other strings like
{exec "cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak"}
{exec "/etc/init.d/ssh reload"}
work perfectly, including the quotation marks.
But now, strings in quotation marks including a slash or a plus sign (maybe also others) just freeze the perl script at this point:
{exec "chmod +x /root/setSSHTimer.sh"}
{exec "sed 's/ClientAliveInterval\ 300/ClientAliveInterval\ 1800/\' /etc/ssh/sshd_config > /etc/ssh/sshd_config.new"}
Both with quotation marks. When rewriting the first to “chmod 770…” it works again.
Any ideas what causes the problem here?
The problem isn’t in the code you’ve shown, which just extracts everything between the
{...}braces, but in the evaluated further code. Please publish this if you are having problems with itNote that you can avoid escaping slashes in a regex simply by using a different delimiter, and a line like
next if m|^//|avoids putting all of the rest of the loop within anifstatementParsing a command line is awkward – because you have to cope with parameters contained within both
"..."and'...'quotes, allowing for escaped quotes within the parameter – but possibleHere is a program that seems to parse all of your example data properly
output
Update
This config format should at least be split over lines so that the quotes and escapes can be removed, for instance
which is very easy both to enter correctly and to parse (although I am sure that your final
sedexample is wrong!)