Unexpected grep Messages During SSH Command Execution

Every time I run this script, it executes but I keep getting ‘grep’ errors and I don’t understand why

#!/bin/bash

if [[ $1 == "" ]]; then

  serverNames=$(grep -v "localhost" /etc/hosts | awk '/^[^#]/{ print $2 }')

  IFS=$'\n' read -rd '' -a servers <<< "$serverNames"
  read -p "type... : " cmd

  for server in "${servers[@]}"; do
      echo "Connecting to $server..."

      ssh -o StrictHostKeyChecking=no "$server" "$cmd"

      if [ $? -eq 0 ]; then
          echo "Command executed successfully on $server"
      else
          echo "Failed to execute command on $server"
      fi
  done

else

  servers=("$@")

  read -p "type... : " cmd

    for server in "${servers[@]}"; do
    echo "Connecting to $server..."

    ssh -o StrictHostKeyChecking=no "$server" "$cmd"

    if [ $? -eq 0 ]; then
        echo "Command executed successfully on $server"
    else
        echo "Failed to execute command on $server"
    fi
done

fi

this is the error

Usage: grep [OPTION]… PATTERNS [FILE]…
Try ‘grep --help’ for more information.

That doesn’t seem to be the entire script. Can you please post it in its entirety?

If it is giving a grep error, and the above doesn’t show grep in the script, means it’s incomplete so nobody here can help until we see the full script.

#!/bin/bash

if [[ $1 == "" ]]; then

  serverNames=$(grep -v "localhost" /etc/hosts | awk '/^[^#]/{ print $2 }')

  IFS=$'\n' read -rd '' -a servers <<< "$serverNames"
  read -p "type... : " cmd

  for server in "${servers[@]}"; do
      echo "Connecting to $server..."

      ssh -o StrictHostKeyChecking=no "$server" "$cmd"

      if [ $? -eq 0 ]; then
          echo "Command executed successfully on $server"
      else
          echo "Failed to execute command on $server"
      fi
  done

else

  servers=("$@")

  read -p "type... : " cmd

    for server in "${servers[@]}"; do
    echo "Connecting to $server..."

    ssh -o StrictHostKeyChecking=no "$server" "$cmd"

    if [ $? -eq 0 ]; then
        echo "Command executed successfully on $server"
    else
        echo "Failed to execute command on $server"
    fi
done

fi

oh, i see
this is entire script

I simulated your script and couldn’t get it to fail on either Rocky 8 or Rocky 9. About the only thing I did do though is format it properly and correct the alignment from what you posted above. So whether that had anything to do with it or not, I’ve no idea.

Results:

[root@rocky8 ~]# ./test.sh 
type... : ifconfig
Connecting to abc...
ssh -o StrictHostKeyChecking=no abc ifconfig
Command executed successfully on abc
Connecting to def...
ssh -o StrictHostKeyChecking=no def ifconfig
Command executed successfully on def

I used echo for the SSH commands to show what it was doing, since servers abc and def don’t exist. But it shows that at least it works as intended. Contents of /etc/hosts:

1.2.3.4 abc
1.2.3.5 def
#1.2.3.6 ghi

Script after fixing:

#!/bin/bash

if [[ $1 == "" ]]; then
  serverNames=$(grep -v "localhost" /etc/hosts | awk '/^[^#]/{ print $2 }')
  IFS=$'\n' read -rd '' -a servers <<< "$serverNames"
  read -p "type... : " cmd

  for server in "${servers[@]}"; do
    echo "Connecting to $server..."
    ssh -o StrictHostKeyChecking=no "$server" "$cmd"
    if [ $? -eq 0 ]; then
      echo "Command executed successfully on $server"
    else
      echo "Failed to execute command on $server"
    fi
  done
else
  servers=("$@")
  read -p "type... : " cmd

  for server in "${servers[@]}"; do
    echo "Connecting to $server..."
    ssh -o StrictHostKeyChecking=no "$server" "$cmd"
    if [ $? -eq 0 ]; then
      echo "Command executed successfully on $server"
    else
      echo "Failed to execute command on $server"
    fi
  done
fi

thanks a lot!
It was caused by the settings in the bashrc of a script that I had configured separately.

1 Like