很难将Perl脚本作为Bash别名运行

很难将Perl脚本作为Bash别名运行,perl,apache,bash,alias,Perl,Apache,Bash,Alias,为了格式化我的apache错误日志,以便更愉快地查看它,我编写了一个快速而肮脏的perl脚本 tail -f /var/log/apache2/error_log | perl -ne '($timeStamp, $error, $hostName, $message) = /^\[([^\]]+)\] \[([^\]]+)\] (?:\[client ([^\]]+)\])?\s*(.*)$/i; # Parse log ($day, $month, $date, $

为了格式化我的apache错误日志,以便更愉快地查看它,我编写了一个快速而肮脏的perl脚本

tail -f /var/log/apache2/error_log | perl -ne 
  '($timeStamp, $error, $hostName, $message) = 
      /^\[([^\]]+)\] \[([^\]]+)\] (?:\[client ([^\]]+)\])?\s*(.*)$/i; # Parse log
   ($day, $month, $date, $time, $year) = 
      $timeStamp =~ m/(\S*) (\S*) (\S*) (\S*) (\S*)$/; # Extract the timestamp
   $message =~ s/, referer: (.*)$/\./;  # Strip the referer references
   $message =~ s/\\n/\n/g; # Replace literal new lines to expand object dumps
   print $time . " " . $date . " " . $month . " | " . $message ."\n";'
我想将脚本添加到Bash别名中,以便可以从终端轻松调用它

e、 g

显然,转义引号开始变得混乱。我还尝试将perl脚本放在自己的文件中,并将其作为别名运行,但为了便于移植,我希望避免在.bash_概要文件之外运行脚本


如何将perl脚本用作bash别名/函数?或者说我是从完全错误的方向来的

您绝对应该使用函数而不是别名:

function te ()
{
    tail -f /var/log/apache2/error_log \
    | perl -ne \
       '($timeStamp, $error, $hostName, $message) = 
          /^\[([^\]]+)\] \[([^\]]+)\] (?:\[client ([^\]]+)\])?\s*(.*)$/i; # Parse log
        ($day, $month, $date, $time, $year) = 
          $timeStamp =~ m/(\S*) (\S*) (\S*) (\S*) (\S*)$/; # Extract the timestamp
        $message =~ s/, referer: (.*)$/\./;  # Strip the referer references
        $message =~ s/\\n/\n/g; # Replace literal new lines to expand object dumps
        print $time . " " . $date . " " . $month . " | " . $message ."\n";
       '
}
改变


谢谢你的快速回复!这就像做梦一样!我以前也试过,但我一定是有过一些古怪的宿醉,这是最重要的。
function te ()
{
    tail -f /var/log/apache2/error_log \
    | perl -ne \
       '($timeStamp, $error, $hostName, $message) = 
          /^\[([^\]]+)\] \[([^\]]+)\] (?:\[client ([^\]]+)\])?\s*(.*)$/i; # Parse log
        ($day, $month, $date, $time, $year) = 
          $timeStamp =~ m/(\S*) (\S*) (\S*) (\S*) (\S*)$/; # Extract the timestamp
        $message =~ s/, referer: (.*)$/\./;  # Strip the referer references
        $message =~ s/\\n/\n/g; # Replace literal new lines to expand object dumps
        print $time . " " . $date . " " . $month . " | " . $message ."\n";
       '
}
alias te=tail -f /var/log/apache2/error_log | perl -ne '...'
alias te='tail -f /var/log/apache2/error_log | perl -ne '\''...'\'''