2

I'm new to bash, and need some simple script. It runs jar, and has to find "RESPONSE CODE:XXX". I need this response code (just XXX). I've try this:

 URL=$1
echo $URL
callResult=`java -jar RESTCaller.jar $URL`
status=$?
if [ $status -eq 0 ]; then
    result=`$callResult >> grep 'RESPONSE CODE' | cut -d':' -f 2`
else
    echo error
fi

I get ./run.sh: line 7: RESPONSE: command not found

What am I doing wrong?

2 Answers 2

2

In this line:

result=`$callResult >> grep 'RESPONSE CODE' | cut -d':' -f 2`

You should be piping output to grep, not redirecting. Change it to this:

result=`$callResult | grep 'RESPONSE CODE' | cut -d':' -f 2`

Also, the syntax is a bit off, and you're better off avoiding backticks when possible. This is even better:

result="$(echo ${callResult} | grep 'RESPONSE CODE' | cut -d':' -f 2)"
Sign up to request clarification or add additional context in comments.

6 Comments

The backquotes in line 3 (definition of callResult) should be straight quotes, also. Unless he meant to echo that and pipe to grep.
@theglauber Good point. I try to avoid backticks and just stick to using the usual "$( ... )" style, as it results in neater code.
thanks, it helped. but as I found, it gives me some xtra output, not just 200, but 200 <!doctype html><html itemscope="itemscope" itemtype="http what's wrong with cut?
What exact output do you get when you remove the | cut -d':' -f 2 ?
I get RESPONSE CODE:200 <!doctype html><html itemscope="itemscope" itemtype="schema.org/WebPage"><head><meta itemprop="image" content=" ....*google site's content
|
1
URL=$1
echo $URL
callResult=`java -jar RESTCaller.jar $URL`
status=$?
if [ $status -eq 0 ]; then
    result=$($callResult 2>&1 grep 'RESPONSE CODE' | cut -d':' -f 2)
else
    echo error
fi

You were piping result to some invalid file name >> means write into file adding on..

2>&1 means redirect stderr to stdin - which is all of its output -

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.