0

When i run the below command i get the output, however it throws

command not found

error. I have no idea why this error appears. Any suggestions?

val1 = ps -ef|grep -i active | wc -l
val2 = 2

echo $val1

if [$val1 -ge $val2]  ; then
        echo "Active MQ process is Running on $HOSTNAME and the process total $(netstat -a | grep 61614 | wc -l)"
else
        echo "Active MQ process appears DOWN $HOSTNAM"
fi

Output below,

 bash-4.1# ./testact.sh
./testact.sh: line 4: val1: command not found
0
./testact.sh: line 5: val2: command not found

Active MQ process is Running on r00du3n0c and the process total 524
bash-4.1#
James Z
  • 12,209
  • 10
  • 24
  • 44
Nilan
  • 103
  • 1
  • 3
  • 14

1 Answers1

1

Run your script through ShellCheck and you'll get this error:

Line 6:
if [$val1 -ge $val2]  ; then
^-- SC1009: The mentioned parser error was in this if expression.
   ^-- SC1035: You need a space after the [ and before the ].
   ^-- SC1073: Couldn't parse this test expression.
                    ^-- SC1020: You need a space before the ].
                    ^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.

Fix that by adding spaces:

if [ $val1 -ge $val2 ]  ; then

Then you'll get more errors:

Line 1:
val1 = ps -ef|grep -i active | wc -l
^-- SC2037: To assign the output of a command, use var=$(cmd) .
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang.
     ^-- SC1068: Don't put spaces around the = in assignments.
              ^-- SC2126: Consider using grep -c instead of grep|wc -l.

Line 2:
val2 = 2
     ^-- SC1068: Don't put spaces around the = in assignments.

As indicated, remove the spaces and fix the command output assignment.

val1=$(ps -ef | grep -i active | wc -l)
val2=2
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Even with the spaces, we'd be getting [SC2086](https://github.com/koalaman/shellcheck/wiki/SC2086) on the new code until quotes are added. – Charles Duffy Aug 03 '17 at 14:56