I’m trying to use the while read
command to create a comma separated table of names, routers, and IP addresses. It should then SSH through that series of routers with the router1 value, input a command, and extract the IP addresses it should return, then write them to a file. It is my understanding that I can do this with read
.
grep -v '^#' /opt/pp1/projectprefixed/peeringinfo > /opt/pp1/projectprefixed/$DATE/peeringinfo.conf
while IFS=, read NREN router1 peering
do
if [ ! -f /opt/pp1/projectprefixed/$DATE/recFromNRENList/$router1 ]
then
ssh -q -n -o StrictHostKeyChecking=no srv_oc_scripts@$router1 "show route recieve-protocol bgp" > $router1
grep -o [[:digit:]]{3}[.]?[[:digit:]]{3}[.]?[[:digit:]]{3}[.]?[[:digit:]]{3} > $router1
fi
done < $router1
The table is a text like that looks like this:
NAME1,mx1.ROUTER1,192.168.1.1
NAME2,mx1.ROUTER2,192.168.65.7
However, it returns an "ambiguous redirect" error.
2
Answers
Redirection is set up before any command execution is done.
This implies that the input redirect send to the while-loop is never updated and will always read the initial value of "router1" as input.
In a simplified form of code, here we create two files, read
foo
in the while-loop and attempt to change the input file of the while-loop mids run.This does not work because the while loop’s input is assigned to the file-pointer connected to foo and you cannot change that.
Where does the error come from? Most likely related to Getting an "ambiguous redirect" error
Most likely, you forgot to assign
router1
before the while-loop or the variablerouter1
, which is local to the while-loop, is empty. This is in agreement with the errors mentioned in the linked question.There are several major logic errors
ssh
will consume stdin. Read from a different file descriptorrouter1
appears to be initialized in theread
command, but is used as an input file to the loop beforehand, and as the ssh remote host, and as a file name you test for existence, and as the output file for the loop commands. I can’t tell how this file is supposed to exist, and what contents it’s supposed to havegrep
will also consume stdin: You probably forgot to specify what input you want forgrep
As a wile stab in the dark, perhaps you want