Solved Shell Scripting Output to File and Screen

Josh Rea

New Member


Messages: 3

Hello all,
I wrote a script to direct all output to a log file, but I'd like to echo certain things during the process of the script to screen output if I can. I haven't been able to find anything related to this searching online, and the man page for sh is cryptic for me (as of now).
Sample of script:
Code:
#!/bin/sh
exec > /path/to/logfile.log
exec 2>&1
rsync command
I'd like to see about printing to screen echo "Sync process started" at the start of the rsync command in the script, and something about it completing after the command executes, but have that print to screen, rather than the log file.
I appreciate all responses in advance!
 

rigoletto@

Daemon
Developer

Reaction score: 1,041
Messages: 2,101

I am not very good at scripting but something like this should work:

Code:
#!/bin/sh
exec > /path/to/logfile.log
exec 2>&1
echo "Sync process started"
rsync command
echo "Sync process done"
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 8,050
Messages: 31,634

You may want to have a look at the tee(1) command.

rsync something remote:something | tee my.log

The tee(1) command basically splits your output two ways, once to the screen and once to a file.

Code:
echo "Starting" | tee -a /path/to/logfile.log
rsync <command> >> /path/to/logfile.log 2>&1
echo "Done" | tee -a /path/to/logfile.log
 
OP
OP
Josh Rea

Josh Rea

New Member


Messages: 3

lebarondemerde -I have tried the echo command like that in the script before, and all that does is print that output to the log file.
SirDice I thank you for the input, but I'd like something that echoes to the screen, as in a progress/status update of the script, rather than redirect to a file.
 

tobik@

Daemon
Developer

Reaction score: 1,386
Messages: 1,909

Save the original stdout and use it later:
Code:
#!/bin/sh
exec 3>&1
exec > logfile.log     
exec 2>&1
echo "Sync process started" >&3
rsync command
echo "Sync process stopped" >&3
 
Top