16

I am trying to execute the following code with an IP address as a parameter from the commandline; however I am getting an error saying - ": line 6: syntax error near unexpected token `echo' "

.

#!/bin/sh
echo $1;
declare -a values=$(ssh -q  jboss@$1 "ps -eo pcpu,pid,user | sort -r -k1 | less | grep jboss");
for value in ${values[*]} do
   echo $value;
done

Please can you help me rectify this error?

Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
user2986175
  • 337
  • 1
  • 3
  • 8
  • 1
    Note that you are missing a pair of parentheses for your array assignment: `declare -a values=( $(ssh -q ... ) )`. Whether or not the output of the remote command is parsed into the correct set of array elements is another question. Also, the `less` in your pipeline is unnecessary at best. – chepner Dec 18 '13 at 19:09
  • To safely iterate over the elements of an array, use `"${values[@]}"` -- with the quotes. See http://stackoverflow.com/a/12316565/7552. To create the array, you need to wrap the values in parentheses: `declare -a values=( $(ssh ...) )`. What you currently have is an array with one element, and that element is a string with words separated by whitespace. – glenn jackman Dec 18 '13 at 19:22

1 Answers1

42

Put a ; in front of the do, or put the do on a new line.

for value in ${values[*]}; do
  echo $value
done

The ; behind "echo $value" isn't needed except if you write the done directly behind it.

Guntram Blohm
  • 9,667
  • 2
  • 24
  • 31