HackThisSite - Extended Basic Mission 09
Challenge
Captain Kirk has coded this Perl script for all his fellow-captains to automate their logging. This way they don't have to record their logs on tape, but they can type them in and archive them. But this log only seems to log one log?! It automatically deletes all previous logs! Fix the script for him, so they can keep their logs again! Captain Kirk 给同僚写了一个自动记日志的 Perl 脚本,但每次只留下一条日志, 之前的全被删掉;把它修好,让日志能留存下来。
关卡页给出完整脚本:
1 | #!/usr/bin/perl |
Solution
脚本把交互内容累加进 $LogText,最后用四条
print STARTREKLOG 落盘:
1 | print STARTREKLOG ' -- START OF LOG -- ' . "\n"; |
写入语句本身没有问题,本次要记的内容也完整。决定上一次的日志还在不在的是更早的那一行:
1 | open(STARTREKLOG, '>/var/log/startrek'); |
Perl 的两参数 open
把模式写在文件名前面:< 读、>
写、>> 追加。>
是截断写:打开时先把文件长度清零,文件指针回到开头,于是每次运行都从空文件开始,上一次的内容在这次运行的第一条
print 之前就没了。题面说的
It automatically deletes all previous logs
正是这个行为。