×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Thanks shaomiao. By this way, I did manage to see the error message:

Can\'t open -f: No such file or directory at /opt2/snort/notify/snortnotify.pl line 1.
ERROR: file doesn\'t exist!

It is weird to me. Because when I manually call \"echo \"pattern\" | /usr/bin/perl /opt2/getdesc.pl >> /var/log/test.output| /usr/bin/perl /opt2/snort/notify/snortnotify.pl -f /opt2/snort/notify/notify.conf

and \"perl /opt2/snort/notify/snortnotify.pl -f /opt2/snort/notify/notify.conf\" has been tested thousand times. I guess this is not a appropriate call in the shell script.
It didn\'t produce any error message and worked fine.
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 一个有关在shell里调用perl的问题,有人能帮我吗?
    • 有, next question?
      • 谢谢你,是这样的.
        我的系统当匹配成功的时候会自动trigger一个shell script:alert.sh,alert.sh里会调用两个perl script, 如下:
        ---------------------------------------------
        #!/bin/sh
        while read line
        do
        echo $line | mail -s "test alert" abc@company.ca;
        echo $line | perl /opt2/getdesc.pl | perl /opt2/snort/notify/snortnotify.pl -f /opt2/snort/notify/notify.conf;
        done
        -------------------------------------------------

        自动匹配到的文本是shell script 和perl 的input.
        我用sh -x alert.sh 然后输入文本,没有问题,程序运行良好.但自动运行时好象没有作用. mail -s 是用来debug的,至少自动trigger alert.sh 没有问题.
        问题在哪里呢?
        • Does "alert.sh" have execution permission?
          • yes. In alert.sh, " mail -s " has been successfully called.
        • what do you mean "auto execute", by CRONTAB? If you use CRONTAB, you cannot use echo to output text to screen, but can redirect to a file
          • NO. It's using a program to do so other than crontab. Crontab is scheduled job only.
            As I said, "mail -s" can be successfully called up so alert.sh has no problem to be automatically called up. I was hung up at perl portion.
            • Try to use full path of perl?
              • Tried but in vain.
                • You mean you already tried the full path for "perl" command , not your perl script?
                  • yes. New script:
                    ---------------------------------------------
                    #!/bin/sh
                    while read line
                    do
                    echo $line | mail -s "test alert" abc@company.ca;
                    echo $line | /usr/bin/perl /opt2/getdesc.pl | /usr/bin/perl /opt2/snort/notify/snortnotify.pl -f /opt2/snort/notify/notify.conf;
                    done
                    -------------------------------------------------
                • What is the error code?
                  • What if I could do some debug?! I didn't see any error, neither did I know where to find the error.
                    • If the code not hang there, type echo $? as soon as the code exit
                • I guess the problem is some over-looked detail.
                  Try something like:

                  #!/bin/sh
                  touch /tmp/content.$$
                  touch /tmp/err.$$
                  while read line
                  do
                  echo $line >> /tmp/content.$$
                  echo $line | /usr/bin/perl /opt2/getdesc.pl | /usr/bin/perl /opt2/snort/notify/snortnotify.pl -f /opt2/snort/notify/notify.conf 1> /tmp/err.$$ 2>&1
                  echo $? >> /tmp/err.$$
                  done

                  Then,
                  cat /tmp/err.<pid>

                  What is the return code? Any error message?

                  cat /tmp/content.<pid> | alert.sh

                  Does the perl portion work fine?
                  • why not just do a "echo input lines | sh -x alert.sh 1>/tmp/alert.sh.$$ 2>&1" ?
                    • He/she has done that from command line and it worked fine.
                      • Thanks shaomiao. By this way, I did manage to see the error message:
                        Can\'t open -f: No such file or directory at /opt2/snort/notify/snortnotify.pl line 1.
                        ERROR: file doesn\'t exist!

                        It is weird to me. Because when I manually call \"echo \"pattern\" | /usr/bin/perl /opt2/getdesc.pl >> /var/log/test.output| /usr/bin/perl /opt2/snort/notify/snortnotify.pl -f /opt2/snort/notify/notify.conf

                        and \"perl /opt2/snort/notify/snortnotify.pl -f /opt2/snort/notify/notify.conf\" has been tested thousand times. I guess this is not a appropriate call in the shell script.
                        It didn\'t produce any error message and worked fine.
        • 估计是你调用perl的时候出的问题,因为你在自动跑 壳程序(shell) 的时候 路径(path) 没有设置,试试这个:
          在你的unix shell里面敲一行 which perl
          然后把那个输出贴到你的 壳程序里面去, 把所有的 perl 命令全部换成这个输出
    • Give me your input, please! Thx
    • When it runs without manual input, how does it get the input? What is your command line?
      • My program will do some pattern matching. Once it matches, it will trigger this alert.sh and pipe matched text to this shell script.
        • I see. replace mail with /usr/bin/mail and perl with /usr/bin/perl. Commend out the perl script to see if mail works. And then replace each perl with a simple perl that writes to a file.. U get the idea.
          • OH, IN YOUR JAVA/PERL/SHELL/SED/AWK PROGRAM, REPLACE "sh" with "/bin/sh" OR EVEN "/bin/bash" which ever is more appropriate.
    • 谢谢大家的指点,我发现问题在这里, shell script里:
      echo $line | /usr/bin/perl /opt2/getdesc.pl | /usr/bin/perl /opt2/snort/notify/snortnotify.pl

      第一个pipe后没有把结果传个第二个pl做为输入.若我改成:
      echo $line | /usr/bin/perl /opt2/getdesc.pl >> /var/log/test.output;
      cat /var/log/test.output | /usr/bin/perl /opt2/snort/notify/snortnotify.pl;

      就可以了. 但是我还是希望用pipe来连接两个perl的输入输出.各位大虾能再帮我想想这个pipe的问题吗? 谢谢了.
      • 让俺瞅瞅你的getdesc,pl, 是不是没有用标准输出 (print) ?
        • 这次终于发现问题所在了.不是上面pipe的问题.其实是:
          echo $line 后系统自动在原来的文本前加了一个<33>(不太明白为什么会加了这么个东西.),由于这个增加,使得perl里不能再匹配到文本了.