flowerpot
flowerpot

Reputation: 519

Too many arguments error in shell script

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.

Upvotes: 0

Views: 6810

Answers (2)

kyrylo
kyrylo

Reputation: 1738

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"

Upvotes: 0

Dominik Honnef
Dominik Honnef

Reputation: 18430

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.

Upvotes: 2

Related Questions