# help creating a log file for shell script



## fred974 (Jan 29, 2014)

Hello all,

I have done a post install script that install various packages.. For example I have:

```
echo "# Updating The Ports Collection"
portsnap fetch extract
rehash
echo "# Enable PKGNG as new package format"
echo 'WITH_PKGNG=yes' >> /etc/make.conf
echo "# convert your /var/db/pkg database to the new pkg format"
pkg2ng
echo "# Install portmaster management tool."
make -C /usr/ports/ports-mgmt/portmaster BATCH=yes OPTIONS_FILE_SET="BASH ZSH" install clean
echo "# Check for outdated port"
portmaster -L
```

What I want to know is how can have create a log file to see id there was any error/warning messages that occurred whilst the script was ran?
Could anyone help.
I'll read any documentation you have to offer.

Thank you


----------



## SirDice (Jan 29, 2014)

You don't need to run rehash with a shell script. That's only needed for interactive csh(1), not for sh(1).

The simplest way is to redirect all the output of the script to a file:
`./myscript.sh > mylogfile.txt`

You can also add redirection to the script itself.


----------



## fred974 (Jan 29, 2014)

Cool thank you @SirDice this will indeed do it.
Since posted the message I tried to do the following:

```
touch ${HOME}/postinstall/logs/logouput
touch ${HOME}/postinstall/logs/logerror
logouput=???
logerror=???
LOGPATH=${HOME}/postinstall/logs
ERRORLOG=${LOGPATH}/${logerror}_`date +%d/%m/%y_%H:%M:%S`.log
OUTPUTLOG=${LOGPATH}/${logouput}_`date +%d/%m/%y_%H:%M:%S`.log
exec 1> ${OUTPUTLOG}
exec 2> ${ERRORLOG}
echo `date +%d/%m/%y_%H:%M:%S` : ----------- START of ${OUTPUTLOG} --------------- 1>
echo `date +%d/%m/%y_%H:%M:%S` : ----------- START of ${ERRORLOG} --------------- 2>
```

Is there any chance you could help me with what to replace the ?? with. 


Thank you


----------



## SirDice (Jan 29, 2014)

Looks like it could be anything, the script simply adds the date to the end. So it's just a common name you want to use for the log file itself.


----------



## tzoi516 (Jan 29, 2014)

What about using `script`?


```
# script /tmp/output.txt
# ./myscript.sh
# exit
# less /tmp/output.txt
```


----------



## worldi (Jan 29, 2014)

+1 for script().

Note that you can also specify the command you want to run. This allows for nice aliases:

```
alias svnup_ports="script -aq /var/log/svnup.ports  svn up /usr/ports"
alias svnup_src="script -aq /var/log/svnup.src  svn up /usr/src"
```


----------



## fred974 (Jan 29, 2014)

tzoi516 said:
			
		

> What about using `script`?
> 
> 
> ```
> ...



Sorry I am not aware of 'script' could you please give more info?

I tried to change my original attemtp an got that:

```
mkdir -p ${HOME}/scripts/scriplogs/postinstall/
LOGPATH=${HOME}/scripts/scriplogs/postinstall/
OUTPUTLOG=${LOGPATH}/logouput_`date +%d/%m/%y_%H:%M:%S`.log
ERRORLOG=${LOGPATH}/logerror_`date +%d/%m/%y_%H:%M:%S`.log
# Redirect all stdout and stderr to respective files
exec 1> ${OUTPUTLOG}
exec 2> ${ERRORLOG}
```

Could anyone critic my last attempt?

Thank you


----------



## Carpetsmoker (Jan 29, 2014)

fred974 said:
			
		

> Sorry I am not aware of 'script' could you please give more info?



Type *man script*.

Tip: You can do this for (almost) all commands, and is probably one of the most useful sources of help on your FreeBSD system.

Type *man man* for general information about manpages.


----------



## fred974 (Jan 29, 2014)

ok... I created a simple shell script

```
#!/bin/sh
#
mkdir -p ${HOME}/scripts/logs/postinstall/
LOGPATH=${HOME}/scripts/logs/postinstall
OUTPUTLOG=${LOGPATH}/logouput_`date +%d/%m/%y_%H:%M:%S`.log
ERRORLOG=${LOGPATH}/logerror_`date +%d/%m/%y_%H:%M:%S`.log
# Redirect all stdout and stderr to respective files
exec 1> ${OUTPUTLOG}
exec 2> ${ERRORLOG}

echo "# Synchronize The Local Clock."
make -C /usr/ports1/net/openntpd/ BATCH=yes install
```
I had the following outcome

```
root@FreeBSD:~/scripts # ./test123.sh
./test123.sh: cannot create /root/scripts/logs/postinstall/logouput_29/01/14_21:56:41.log: No such file or directory
```
Not sure how to solved that ...

on my way to read man script


----------



## Carpetsmoker (Jan 29, 2014)

The directory doesn't exist. Use `mkdir -p` to create it. For example: `mkdir -p "$(dirname "$OUTPUTLOG")"`


----------

