2013-06-25 42 views
0

我想在etter.conf文件中取消註釋行168。該命令在終端中運行OK,但在perl中嘗試時出現錯誤。使用sed替換perl中的行

system ("sed -i '168s/.*/redir_command_on = "iptables -t nat -A PREROUTING -i %iface -p tcp --dport %port -j REDIRECT --to-port %rport"/' /etc/etter.conf"); 

錯誤是:

Bareword found where operator expected at ./attack.pl line 135, near 
""sed -i '168s'/.*'/redir_command_on = "iptables" 

我覺得是有一些做的特殊字符和轉義。

+0

您需要轉義嵌套引號。 Perl認爲iptables應該是可變的。 –

+0

命令中的雙引號正在終止perl字符串。 – Barmar

+3

你是否有理由對'sed'進行炮擊? Perl完全可以自行更新文件。 – Barmar

回答

5

所以Perl是一起分析,並找到一個字符串

system ("sed -i '168s/.*/redir_command_on = " 
     ^        ^
     |         | 
     +-----------------------------------+ 

下一步是什麼應該是)或運營商,但它的iptables。你沒有正確地形成你的字符串文字。切換定界符將這樣的伎倆:

system(q{sed -i '168s/.*/redir_command_on = "..."/' /etc/etter.conf}) 

q{...}相同'...'

順便說一句,使用「列表形式」 system是更好,因爲它避免了啓動和使用shell不必要,

system('sed', '-i', '168s/.*/redir_command_on = "..."/', '/etc/etter.conf') 
+0

用於推薦使用system()和參數列表 –

+0

不錯,你也可以用'system(qw {...'來替換'system(q {...'來自動獲取列表表單 – steabert

+0

@ steabert,不會,你會得到一個列表,但它不會是正確的命令。 – ikegami

1

您不能在雙引號字符串中嵌套裸雙引號。 Perl可以使用更多的quoting operators

# Instead of 
system ("sed -i '168s/.*/redir_command_on = "iptables -t nat -A PREROUTING -i %iface -p tcp --dport %port -j REDIRECT --to-port %rport"/' /etc/etter.conf"); 

# use 
system (q{sed -i '168s/.*/redir_command_on = "iptables -t nat -A PREROUTING -i %iface -p tcp --dport %port -j REDIRECT --to-port %rport"/' /etc/etter.conf}); 
#-------^^------------------------------------------------------------------------------------------------------------------------------------------------^