I have a .sh script that I run from the command line and also put into a cron job. I need to see the output on the screen and read a generated log file.
There are a couple of ways to approach this:
1) a file like: In this case the output goes to t.log but not on the screen
#!/bin/ksh exec 1>t.log 2>error.log>&1 printf "UNIX output displayed here\n" ls -l svrmgrl <<! @$ID_DIR/vant8i_internal_id.sql; exit !
2) a file like: Using the tee command. But in this case I have to put
| tee -a t.log
at every command I want to see, my real file is too long.
#!/bin/ksh printf "UNIX output displayed here\n" | tee -a t.log ls -l|tee -a t.log svrmgrl <<! |tee -a t.log @$ID_DIR/vant8i_internal_id.sql; exit !
3) Final option. This uses the unix script command to output everything on the screen to the file t.log The script session must be terminated with a CTRL-D. The problem here is I don't know how to represent a ^D in a script. Can I execute it as an octal (004) but how?
#!/bin/ksh script t.log printf "UNIX output displayed here\n" ls -l svrmgrl <<! @$ID_DIR/vant8i_internal_id.sql; exit ! ^D
> The script session must be terminated with a CTRL-D.
Actually, no. Script terminates on EOF or when the child process terminates. ^D is just how *you* type EOF.
>#!/bin/ksh >script t.log >printf "UNIX output displayed here\n" >ls -l >svrmgrl <<! >@$ID_DIR/vant8i_internal_id.sql; >exit >! >^D
Imagine a script
#!/bin/ksh cat ^D
Will this work? Of course not! "cat" doesn't read from the script - the script is being processed by /bin/ksh. "cat" reads from the script's input.
What you've just done is write a script to run script (with output to t.log)… But it runs script with no input specified, so it runs on stdin.
Try, perhaps
script t.log <<EOS printf "hi mom\n" ls -l # substitute your preferred program here cat<<EOF blah exit EOF exit EOS
The last "exit" is because most shells terminate on exit.