Reputation:
I'm trying to run a grep command on a log file to grab a url to use for some functions. The problem is it keeps giving me a No such file or directory
error whenever I try to run it. I thought this was bizarre so I prefixed it with an ls
command to see if it did exist and lo and behold it is indeed there. Can someone tell me what the issue is, I have a feeling it's a syntax issue I'm not aware of but I can't figure it out.
#!/bin/bash
for i in `seq 1 3` ;
do
ssh user@member${i}.domain.com << EOF
cd /home/path/member${i}/logs
ls #I've printed the output of this below
test=$( grep -oP "http:.*microwebapp/$" "messages.log" )
echo "Test variable = ${test}"
exit
EOF
done
The output of the ls
command is the below:
console.log
messages.log
status.log
trace.log
So I'm really confused about what's going on, I've also tried replacing "messages.log"
with "./messages.log"
to no avail.
EDIT: here's the full output for one iteration of the loop:
grep: ./messages.log: No such file or directory
Pseudo-terminal will not be allocated because stdin is not a terminal.
console.log
messages.log
status.log
trace.log
Test variable =
I suppose it's a little odd that the grep
command throws an error before the ls
command runs but I don't have an explanation for that either
Upvotes: 0
Views: 364
Reputation: 246807
Because of your "plain" heredoc, these lines execute on your local machine, before the shell hands control to ssh
test=$( grep -oP "http:.*microwebapp/$" "messages.log" )
echo "Test variable = ${test}"
Try:
ssh user@member${i}.domain.com << EOF
cd /home/path/member${i}/logs
ls #I've printed the output of this below
test=\$( grep -oP "http:.*microwebapp/$" "messages.log" )
#....^
echo "Test variable = \$test"
#.....................^
exit
EOF
By "plain" heredoc, I mean the terminating word is unquoted. One can put single quotes around the word, and that effectively single quotes the whole heredoc:
ssh remote_host <<'END'
#.................^...^
h=$(hostname)
d=$(date)
echo The time at $h is $d
END
You can't do that in your code because you rely on expanding the local var $i
to change to the correct directory.
Upvotes: 6