[help] Writing to /opt/local/var/log/install_setup.log ?

Bill Hernandez ms at mac-specialist.com
Fri May 2 18:35:19 PDT 2008


On May 2, 2008, at 7:47 PM, Bryan Blackburn wrote:

> On May 2, 2008, at 5:36 PM, Bill Hernandez wrote:
>
>> I am trying to write a running command log to : /opt/local/var/log/ 
>> install_setup.log
>> but doing either below :
>>
>> shell> $ sudo echo "${TS} $1" >> $BASH_CMD_LOG
>> shell> $ sudo su -m root echo "${TS} $1" >> $BASH_CMD_LOG
>>
>> do not work...
>>
>
> This fails since sudo applies to the command, but redirection (>,  
> >>, and |) is done in your shell, hence that isn't run by sudo (in  
> fact, it's what is running sudo...).
>
> ...
>>
>> Anybody know how to write to the log using test_one() without  
>> having to alter file/dir permissions, or use any of the  
>> alternatives mentioned above...
>>
>
> If you definitely don't want to change permissions on the directory  
> or just the log file so you can write as your own user, one option  
> would be to use tee:
>
> $ sudo port install mycoolport | sudo tee -a /opt/local/var/log/ 
> port.log
>
> This will send the port output to your terminal and to the log file  
> (-a tells tee to append, not overwrite, just like >>).  And since  
> you're running tee with sudo, it has the permission to write to that  
> location.
>
> Bryan


Bryan,

Thanks a million, this now works very well....

# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
#!/bin/bash

# script_name : test_one

BASH_CMD_LOG=/opt/local/var/log/install_setup.log

# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
function bh_log( )
{
	TS=$(date +\[%Y.%m.%d\]\(%I.%M.%p\))
	sudo echo "${TS} $1" | sudo tee -a $BASH_CMD_LOG
	# sudo echo "${TS} $1" >> $BASH_CMD_LOG  # Generates Permission denied
	# sudo su -m root echo "${TS} $1" >> $BASH_CMD_LOG  # Generates  
Permission denied
}
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
sudo echo
bh_log "MacPorts Setup Start..."
# <snip> ... </snip>
bh_log "MacPorts Setup Finish..."

open ${BASH_CMD_LOG}

# END OF SCRIPT ( test_one )
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+




More information about the macports-users mailing list