简体   繁体   English

将命令行参数传递到.sh(shell)文件

[英]passing commandline parameter to .sh (shell) file

I am executing shell file like this in terminal: 我正在终端中执行这样的shell文件:

./cherrypicker.sh input.txt

input.txt contains input text. input.txt包含输入文本。

My purpose is to pass input text directly as command line argument from web interface 我的目的是从Web界面直接将input text作为命令行参数传递

I checked cherrypicker.sh file, to get some clue. 我检查了cherrypicker.sh文件,以获取一些线索。 it has 它有

tools/pos/pos.sh tools/pos $1 > $1.pos 2> /dev/null

If $1 would have been text from input.txt then I could have passes text directly. 如果$1是来自input.txt文本,那么我可以直接传递文本。 But when I do echo $1 it give input.txt . 但是当我echo $1它给出了input.txt

I could not understand what is > indicates here and also 2 and /dev/null 我不明白什么是>并在此处指示2/dev/null

Any explaination would be much appreciable. 任何解释都是很有意义的。 I checked about .sh file but articles says it's shell file equavalent to .bat file 我检查了.sh文件,但文章说它是等同于.bat文件的shell文件

Cherrypicker.sh Cherrypicker.sh

#!/bin/bash

echo "running stanford pos tagger..."
tools/pos/pos.sh tools/pos $1 > $1.pos 2> /dev/null

echo $1.pos

echo "running stanford ner tagger..."
tools/ner/ner.sh tools/ner $1 > $1.ner 2> /dev/null

echo "running charniak parsing..."
java MakeCharniakInput $1
tools/charniak-parser/PARSE/parseIt -l300 tools/charniak-parser/DATA/EN/ $1.charniakIN > $1.charniak

echo "running minipar parsing..."
tools/minipar/pdemo/pdemo -p tools/minipar/data/ < $1 > $1.minipar

echo "detecting mentions..."
java MentionDetection $1
tools/crf++/crf_test -m modelmd $1.crf > $1.pred
java CreateNPSpan $1 $1.pred


# if [[  $1 = "mp" ]]
# then
#   echo "creating feature file...."
#       java -cp .:edu.mit.jwi.jar CherryPick mp raw.txt
#       echo "classifying clusters using $1 model....."
#       tools/svmmentionpair/svm_classify raw.txt.mpsvm modelmp raw.txt.mppred
#       java MakeCluster raw.txt raw.txt.mppred
#   elif  [[ ( $1 = "mr" ) || ( $1 = "cr" )  ]]
#   then
    echo "creating feature file...."
    java -cp .:edu.mit.jwi.jar CherryPick cr $1
    echo "classifying clusters using cr joint model....."
    tools/svmrank/svm_classify $1 modelrank > $1.entities
#   else
#       echo "cannot classify clusters using *mysterious* model....."
#   fi


echo "creating output....."
java MakeResponse $1

1) > and 2> manipulate standard output and standard error stream redirection respectively, so your output goes into $1.pos and error is redirected to /dev/null (discarded) 1)>和2>分别操作标准输出和标准错误流重定向,因此您的输出将进入$ 1.pos,并且错误将重定向到/ dev / null(已废弃)

2) if you want to feed the content of a file as input, then you can redirect the file as input, eg: 2)如果您想将文件的内容作为输入,则可以将文件重定向为输入,例如:

tools/pos/pos.sh tools/pos < $1 > $1.pos 2> /dev/null

or through a pipe: 或通过管道:

cat $1 | tools/pos/pos.sh tools/pos > $1.pos 2> /dev/null

3) if you want the file contents as an argument (I hope input.txt is just one line), then try this: 3)如果您希望文件内容作为参数(我希望input.txt只是一行),请尝试以下操作:

tools/pos/pos.sh tools/pos `cat $1` > $1.pos 2> /dev/null

or you can try xargs to execute your command once per line: 或者您可以尝试xargs每行执行一次命令:

cat $1 | xargs -I myargs tools/pos/pos.sh tools/pos myargs >> $1.pos 2> /dev/null

here >> means standard output is appended to the same file. 这里>>表示标准输出附加到同一文件。

Command: 命令:

   tools/pos/pos.sh tools/pos $1 > $1.pos 2> /dev/null

Explanation: 说明:

tools/pos/pos.sh  - script

tools/pos         - Positional argument 1 for pos.sh

$1                - Positional argument 2 for pos.sh

$1.pos            - Is a file which will hold the standard output of pos.sh

/dev/null         - is a null file which will hold standard error

The tools/pos/pos.sh takes two postional arguments in this case tools/pos & $1(input.txt) does its work 在这种情况下,tools / pos / pos.sh接受两个位置参数tools/pos$1(input.txt)进行工作

and redirects the standard output of tools/pos/pos.sh to file $1.pos(input.txt.pos) ,the part > $1.pos of the command does this and 并将tools/pos/pos.sh的标准输出重定向到文件$1.pos(input.txt.pos) ,命令的> $1.pos部分执行此操作,并且

the part 2> /dev/null of the above command redirects the standard error to /dev/null 上面命令的第2> /dev/null部分2> /dev/null将标准错误重定向到/dev/null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM