user123
user123

Reputation: 5407

passing commandline parameter to .sh (shell) file

I am executing shell file like this in terminal:

./cherrypicker.sh input.txt

input.txt contains input text.

My purpose is to pass input text directly as command line argument from web interface

I checked cherrypicker.sh file, to get some clue. 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. But when I do echo $1 it give input.txt.

I could not understand what is > indicates here and also 2 and /dev/null

Any explaination would be much appreciable. I checked about .sh file but articles says it's shell file equavalent to .bat file

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

Upvotes: 1

Views: 2769

Answers (2)

mariusm
mariusm

Reputation: 1668

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)

2) if you want to feed the content of a file as input, then you can redirect the file as input, e.g.:

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:

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:

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.

Upvotes: 2

Ram
Ram

Reputation: 1115

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

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

the part 2> /dev/null of the above command redirects the standard error to /dev/null

Upvotes: 2

Related Questions