0

As follow, I only want the command echo "this is to stdout" output to my screen instead of to the file ok.log, how can I do?
I search for the usage of exec shell command, but without result, please advice me

[root@161 tmp]# bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.

[root@161 tmp]# cat 2.sh
#!/bin/bash
exec 1>ok.log
exec 2>error.log
#exist dir
ls /home/
#no exist dir
ls /etca/
#to stdout
echo "this is to stdout"
#other cmds
...
jython.li
  • 84
  • 2
  • 10

1 Answers1

1

You can save the original stdout to a temporary file descriptor before you redirect it. In this example I use file descriptor 3.

exec 3>&1
exec 1>ok.log
echo "This will go to ok.log"
echo "This will go to the original stdout" >&3
Tom Shaw
  • 3,702
  • 15
  • 23
  • You're right, there is another way:`echo "this is to stdout" > /dev/tty` – jython.li Aug 03 '16 at 08:06
  • That `/dev/tty` method is a little bit different and not ideal. It would give surprising results if the script was run with stdout already redirected somewhere else. For example, if you ran `./script.sh >/dev/null` you would be surprised to see output on the terminal. If you ran it as a cron job the output wouldn't be in the cron result email. – Tom Shaw Aug 03 '16 at 08:23
  • Thks, learned about – jython.li Aug 04 '16 at 02:31