bash脚本的未知错误!

时间:2010-11-21 16:36:02

标签: perl bash scripting

所以我有一个perl脚本,它接受2个参数。

  1. 一个目录,指向我有兴趣搜索的“.txt”文件
  2. 一个文件,其中包含我已转换为数组的文字。
  3. Perl文件(perlstemmer.pl):

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    open(ALLFILES,"/Users/mymacbook/saver/rssFolders/txt") or die "Can't Open: $!\n";
    open(TRIGGERFILE,"/Users/mymacbook/trigger.txt") or die "Can't Open: $!\n";
    
    my $line=<>;
    my %words;
    my $i;
    my @triggers =<TRIGGERFILE>;
    
    while(<TRIGGERFILE>) #read each line into $_
    {
       chomp @triggers;  # Remove newline from $_
       push @triggers, $_; # add the line to @triggers
       #/puts all txt from trigger.txt into the array @triggers. /
    }
    
    while($line ne "")
    {
     %words = split(//, $line);
    }
    
    foreach $i(@triggers)
    {
     if (exists $words{$i})
     {
      return 1;
     } 
     $line= <>; 
     return 0;
    }
    

    所以如果文件中有一个来自@triggers数组的单词,那么这个脚本将返回1,如果没有,则返回0 ....

    Bash脚本organise.sh

    然后bash脚本读入它是否有1:

    #!/bin/sh
    DIR=$5
    
    for a in `ls $DIR*`
       do
       b=`basename $a`
       perlStemmer.pl $a >> tmp
    
       $b = filter.pl tmp
       if [$b == 1]; then
          cp $a ../crimeStories/$b
          echo "$a describes crime"
       else
           echo "$a does NOT describe crime"
       fi
    
     done;
    exit 0;
    

    - &GT;因此,如果文件返回1,则复制文件$ a并打印:它确实描述了该文件 否则......如果它返回0 说它不描述犯罪并且不做任何事情: - )

    我的问题

    当我尝试运行时,我得到了一个疯狂的终端:

    >./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: perlStemmer.pl: command not found
    ./organise.sh: line 11: [perlStemmer.pl: command not found
    perlStemmer.pl does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: version.plist: command not found
    ./organise.sh: line 11: [version.plist: command not found
    version.plist does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: Jennie:: command not found
    ./organise.sh: line 11: [Jennie:: command not found
    Jennie: does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: Assignment: command not found
    ./organise.sh: line 11: [Assignment: command not found
    Assignment does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: 1.doc: command not found
    ./organise.sh: line 11: [1.doc: command not found
    1.doc does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: Document.pdf: command not found
    ./organise.sh: line 11: [Document.pdf: command not found
    Document.pdf does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: Gers: command not found
    ./organise.sh: line 11: [Gers: command not found
    Gers does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: polo's: command not found
    ./organise.sh: line 11: [polo's: command not found
    polo's does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    ./organise.sh: line 10: contact: command not found
    ./organise.sh: line 11: [contact: command not found
    contact does NOT describe crime
    ./organise.sh: line 8: perlStemmer.pl: command not found
    

    perlStemmer确实存在我尝试添加它的完整路径但是程序似乎只搜索perlStemmer.pl所在的文件夹。

    我可以看到它似乎是拆分文件名和文件夹名。

    任何人都知道我做错了什么或可能导致这种情况继续循环?

    我也得到了这个:

      This (the Directory node) gives a menu of major topics. 
      Typing "d" returns here, "q" exits, "?" lists all INFO commands, "h" 
      gives a primer for first-timers, "mEmacs<Return>" visits the Emacs topic,
      etc.
      In Emacs, you can click mouse button 2 on a menu item or cross reference
      to select it.
      --- PLEASE ADD DOCUMENTATION TO THIS TREE. (See INFO topic first.) ---
    
    * Menu: The list of major topics begins on the next line.
    
    Emacs
    * Ada mode: (ada-mode). The GNU Emacs mode for editing Ada.
    * Autotype: (autotype). Convenient features for text that you enter frequently
                              in Emacs.
    * CC Mode: (ccmode).   Emacs mode for editing C, C++, Objective-C,
                              Java, Pike, and IDL code.
    * CL: (cl).             Partial Common Lisp support for Emacs Lisp.
    * Dired-X: (dired-x).   Dired Extra Features.
    * EUDC: (eudc).   A client for directory servers (LDAP, PH)
    * Ebrowse: (ebrowse).   A C++ class browser for Emacs.
    * Ediff: (ediff).       A visual interface for comparing and merging programs.
    * Emacs: (emacs).       The extensible self-documenting text editor.
    * Emacs FAQ: (efaq).    Frequently Asked Questions about Emacs.
    * Emacs MIME: (emacs-mime).   The MIME de/composition library.
    * Eshell: (eshell).     A command shell implemented in Emacs Lisp.
    * Forms: (forms).       Emacs package for editing data bases
                              by filling in forms.
    * Gnus: (gnus).         The newsreader Gnus.
    * IDLWAVE: (idlwave).   Major mode and shell for IDL and WAVE/CL files.
    
    * MH-E: (mh-e).         Emacs interface to the MH mail system.
    * Message: (message).   Mail and news composition mode that goes with Gnus.
    * PCL-CVS: (pcl-cvs).   Emacs front-end to CVS.
    * RefTeX: (reftex).     Emacs support for LaTeX cross-references and citations.
    * SC: (sc).             Supercite lets you cite parts of messages you're
                              replying to, in flexible ways.
    
    
    
    * Speedbar: (speedbar).        File/Tag summarizing utility.
    * VIP: (vip).           An older VI-emulation for Emacs.
    * VIPER: (viper).       The newest Emacs VI-emulation mode.
                              (also, A VI Plan for Emacs Rescue
                               or the VI PERil.)
    

    在终端打印!!

2 个答案:

答案 0 :(得分:1)

对于实际问题,这个问题非常冗长且多余, 由这条线表示:

./organise.sh: line 8: perlStemmer.pl

您包含的Perl脚本完全不相关,因为它不是偶数 运行。从一个问题开始,然后轮流解决它们,所以你 不要对其他无法解决的问题感到不知所措。

您的bash脚本找不到perlStemmer.pl。这表明你不在 运行脚本时正确的目录为(因为您没有提供任何目录) 路径信息),它只会查看$PATH中的目录。

在继续之前,您可能需要检查一些错误情况 执行剩余的bash脚本,好像第一部分没有 工作,你只是为了剩下的事情而得到垃圾(正如你所看到的那样)。


顺便提一下,您的脚本存在一些基本问题,会阻止它执行任何有用的操作:

my @triggers =<TRIGGERFILE>;
while(<TRIGGERFILE>) #read each line into $_

这会将文件的全部内容读入@triggers,所以这一次 循环将立即终止,因为您已经在文件句柄的文件末尾。

while($line ne "")

你只读了一行$line而不读另一行,所以这一行 将永远循环或不做任何事,取决于是否第一行 你在stdin上管道的内容是一个空字符串(可能是前者,因为你 没有从中取消换行符。)

答案 1 :(得分:1)

我同意以太,你提供了太多的信息,并且第一个错误行告诉你大部分问题是什么。

但是,有几个未提及的可能问题:

  • Perl脚本不可执行;使用chmod +x perlStemmer.pl
  • Perl脚本不在PATH的目录中;可能会添加'。'到PATH的末尾,或使用路径告诉shell脚本在哪里找到它:
    • export PATH=$PATH:.
    • ./perlStemmer.pl

请注意,在shell脚本中,通常使用退出状态0表示成功,使用1表示失败。表面上看,你的Per脚本反转了 - 使用1表示发现了某些东西,0表示它不是。 OTOH,你的脚本实际上并没有使用'perlStemmer.pl'的退出状态,所以它并不重要。


还有一些奇怪的shell语法,包括一些流浪(多余但不正确)的冒号。

但是,开始时会出现一些问题:

DIR=$5

for a in `ls $DIR*`
   do
   b=`basename $a`
   perlStemmer.pl $a >> tmp

   $b = filter.pl tmp
   if [$b == 1]; then

本身的DIR分配是好的(虽然它是可悲让人键入5个参数,当你只打算使用最后,看来)。

ls命令可能无法实现您的想法;如果$5是一个目录名,那么你所列出的是所有以给定名称开头的目录,而不是子目录中的所有文件......它最终有些类似,但不一样作为ls $DIR/*,这可能是你的意思。

接下来的三行都行。在将来的某个时候,当您更好地掌握基础知识时,我们可以讨论trap以确保tmp不会被遗忘;现在,这是一种不必要的转移。

下一行非常糟糕:

$b = filter.pl tmp

如果b当前包含abcd,则此行尝试使用参数'=','filter.pl'和'tmp'执行命令abcd。这不太可能是你的想法;它也可能是你的一个文件被称为'info',当这一行执行时,它会执行'info'命令 - 这是你问题的最后一部分。

看起来你可能正在寻找在perlStemmer.pl的输出上运行'filter.pl'的结果的退出状态 - 下一行表明了这一点。我们假设你也有为该程序整理的PATH和权限。

测试线也严重破损;如果b包含'abcd',那么它将尝试执行命令'[abcd',并且可能找不到它。一般来说,方括号周围需要空格。

修改后的shell脚本

所以,你可能想要这样的东西,假设'filter.pl'在文件描述犯罪时返回0(成功),否则返回非零(失败):

#!/bin/sh
DIR=$5

for a in `ls $DIR/*`
do
   b=`basename $a`
   perlStemmer.pl $a >> tmp
   if filter.pl tmp
   then
       cp $a ../crimeStories/$b
       echo "$a describes crime"
   else
       echo "$a does NOT describe crime"
   fi
done
exit 0

并且,正在使用trap

#!/bin/sh
DIR=$5
trap "rm -f tmp; exit 1" 0 1 2 3 13 15

for a in `ls $DIR/*`
do
   b=`basename $a`
   perlStemmer.pl $a >> tmp
   $b = filter.pl tmp
   if filter.pl tmp
   then
       cp $a ../crimeStories/$b
       echo "$a describes crime"
   else
       echo "$a does NOT describe crime"
   fi
done
rm -f tmp
trap 0
exit 0