0

I'm trying to write the output (error or success) to a file. With other commands I can do this without issue however the below commands will only write to the terminal but not to a file. If I run directly in the terminal I can see the output on the screen and the file's will be created but will be empty.

The commands are

sudo ip r add default via 192.168.1.254 > outfile.txt >&1
sudo ip r add default via 192.168.1.254 | tee -a outfile.txt
sudo /sbin/route add default gw 192.168.1.254 | sudo tee -a outfile.txt

These are going in a boot script (headless setup). I'm not able to simulate the boot conditions from within the terminal directly so just running them directly doesn't necessarily give me the output that happens in boot conditions meaning I can't work out why/whats causing them to fail.

The output when I run directly in the terminal is RTNETLINK answers: Network is unreachable for sudo ip r add default via 192.168.1.254 and SIOCADDRT: Network is unreachable for sudo route add default gw 192.168.1.254which is expected in those conditions

  • Please check the gw 192.168.1.254 reachability from your server – EBIN GLADSON Sep 25 '18 at 10:11
  • That's what I'm trying to work out. The script is about detecting a gateway change so the old one isn't accessible but the new one (added from a list) should be however it's not adding the new one for some reason so I needed to work out what error it's coming back when it's running during the boot process when trying to add the new one. – Josh Sep 25 '18 at 10:14
0

For bash

ip r add default via 192.168.1.254 &> outfile.txt

or the standard form for other shells:

ip r add default via 192.168.1.254 >outfile.txt 2>&1

from man bash:

   Redirecting Standard Output and Standard Error
       This  construct allows both the standard output (file descriptor 1) and
       the standard error output (file descriptor 2) to be redirected  to  the
       file whose name is the expansion of word.

       There  are two formats for redirecting standard output and standard er‐
       ror:

              &>word
       and
              >&word

       Of the two forms, the first is preferred.  This is semantically equiva‐
       lent to

              >word 2>&1
  • hmm I thought I had tried that yesterday - must have been tired. &>word worked - at least in the terminal. I'll try in the boot script shortly. – Josh Sep 25 '18 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.