0

I have to make ssh request for different nodes based on given IP.

different ssh commands are stored in node.txt as below

0.0.0.0 a   ssh -t user@0.0.0.0  sudo -u  node0  /path/script.sh
0.0.0.1 b   ssh -t user@0.0.0.1  sudo -u  node1  /path/script.sh
0.0.0.2 c   ssh -t user@0.0.0.2  sudo -u  node2  /path/script.sh

I try to grep the needed ssh command like this

comm=$(grep 0.0.0.2 node.txt | grep c | cut -f3)

when I run

status=$($comm)

the following error appears:

/path/script.sh not found

while if I hard coded the command in the script itself it work correctly,

comm='ssh -t user@0.0.0.2  sudo -u  node2  /path/script.sh'
status=$($comm)

what could be the problem here?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Are you sure that's the [full error message](https://meta.stackoverflow.com/q/359146/4518341)? It looks like it's missing a colon at least. – wjandrea Jul 30 '22 at 16:14
  • I suspect you have Windows line-endings in `node.txt`. Try `echo "$comm" | xxd` and see if there's a `0d` in the output. In that case, see [How to convert DOS/Windows newline (CRLF) to Unix newline (LF)](/q/2613800/4518341). – wjandrea Jul 30 '22 at 16:18

1 Answers1

-1

@Karim Ater
Try the following at your system:
    comm=$(grep 0.0.0.2 node.txt | grep c | cut -f3)
    $ echo $comm
Hence I tried
    comm=$(grep 0.0.0.2 node.txt | grep c | sed "s/.*ssh/ssh/;")
    $ echo $comm
I cannot confirm the contents of node.txt to know related delimiter. Hence I used sed instead of using cut.