2

I want to write script in bash (I start programming in bash), but i have error, i try everything, and still i don't know how to repair this.

#!/bin/bash
cap=0
cat|while read line
  do
  read a
    if[$a -gt $cap]
       then echo "MORE"
fi
done

That's easy script, but i'm newbie in bash, please someone help me ;)

5
  • 2
    Explain what you are trying to accomplish with your script. Commented May 1, 2012 at 17:14
  • What are you trying to accomplish? Commented May 1, 2012 at 17:15
  • 1
    First off, $line already contains the current line that was read from the input file, so you don't need the second read a or else you're going to miss half the lines of the input file. Commented May 1, 2012 at 17:16
  • 1
    Second you don't need the cat|, the while read will read from stdin without that. Commented May 1, 2012 at 17:16
  • There are many problems with this code. Commented May 1, 2012 at 17:23

1 Answer 1

8

The syntax error is here:

if[$a -gt $cap]

You need whitespace, and quotes (to be safe with content with spaces or empty values):

if [ "$a" -gt "$cap" ]

Somewhat better would be to use the [[ test mechanism, which is syntax (rather than [, which is a command), and doesn't need double quotes to be safe:

if [[ $a -gt $cap ]]

Even better than that would to be use a math context, which lets you use traditional mathematical operators and automatically dereferences variables:

if (( a > cap )) ; then ...
fi
Sign up to request clarification or add additional context in comments.

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.