0

I am trying a simple shell script like the following:

#!/bin/bash
up_cap=$( cat result.txt | cut -d ":" -f 6,7 | sort -n | cut -d " " -f 2 | sort -n)
down_cap=$( cat result.txt | cut -d : -f 6,7 | sort -n | cut -d " " -f 6| sort -n)
for value in "${down_cap[@]}";do
        if [ $value > 80000 ]; then
                cat result.txt | grep -B 1 "$value"
        fi
done

echo " All done, exiting"

when I execute the above script as ./script.sh, I get the error: ./script.sh: line 5: [: too many arguments All done, exiting

I have googled enough, and still not able to rectify this.

3
  • What is the format of the text in result.txt? Commented Nov 27, 2011 at 17:40
  • here is the sample data in result.txt: /scratch/abcd/traces/shaper/parser/traces/194.105.3.30_1268072907_0.300000delta.txt Upstream: 92345 duration: 0 nlines 0 Downstream:14134 duration: 0 nlines 0...several sets like the above. I am trying to parse and extract upstream and downstream capacity , and grep those lines for which these values are > 80000 Commented Nov 27, 2011 at 17:52
  • Comments are a poor medium for sharing formatted text files. Should probably put it on a pastebin. Commented Nov 27, 2011 at 18:05

2 Answers 2

2

You want

if [ "$value" -gt 80000 ]; then

You use -gt for checking if A is bigger than B, not >. The quotation marks I merely added to prevent the script from failing in case $value is empty.

Sign up to request clarification or add additional context in comments.

2 Comments

But now there is another problem. if ["$value" -gt 80000] returns true for all $value. Is there anything on the string vs numeric comparison?
@linuxAddict: -gt is numeric comparison; for string "greater than" makes no sense.
0

Try to declare variable $value explicitly:

declare -i value

So, with the dominikh's and mine additions the code should look like this:

#!/bin/bash
up_cap=$( cat result.txt | cut -d ":" -f 6,7 | sort -n | cut -d " " -f 2 | sort -n)
down_cap=$( cat result.txt | cut -d : -f 6,7 | sort -n | cut -d " " -f 6| sort -n)
for value in "${down_cap[@]}";do
  declare -i value
  if [ $value -gt 80000 ]; then
    cat result.txt | grep -B 1 "$value"
  fi
done

echo " All done, exiting"

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.