# trying to make a script to capture command output



## phyro (Feb 8, 2012)

Hey all,

I'm trying to make a script to execute a bunch of commands and output the results to a text file. I managed to get most of it done except, commands with arguments when entered by themselves. I want to find a way to also output the arugments if possible.

Here's an example

```
awk | tee -a /Desktop/test.txt | echo "******** AWK ********" >> /Users/name/Desktop/test.txt
```

text.txt looks like:

```
******** AWK ********
```
when you type awk without arguments you get something like this..

```
$ awk
usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
$
```
*I* need text.txt to output the arguments as above. If anyone could please let me know if this is possible that would be super awesome.

Thank You.


----------



## UNIXgod (Feb 8, 2012)

Doesn't script()(1) already do this?

EDIT:

here is a tutorial:
https://forums.freebsd.org/showthread.php?t=17309&highlight=script


----------



## J65nko (Feb 8, 2012)

If you want the error output to go to the file you have to redirect stderr to stdout.
In a script this can be done with the 'exec' command. That is easier then adding the redirect to each command. In the following script all normal output (stdout) as well as any error messages (stderr) are being redirect to the file *logfile*.


```
#!/bin/sh

[b]exec >logfile 2>&1[/b]


cat <<END
--------------- dmesg -----------
$(dmesg)

END
```

If you want the arguments then you can echo the special variable "$@' or "$*".


```
cat show-args                                                
#!/bin/sh

echo "My arguments or parameters are: $@"
```
A sample run:

```
$ sh show-args John Lisa Mary
My arguments or parameters are: John Lisa Mary
```


----------



## phyro (Feb 8, 2012)

I'm not sure what's up but it still won't echo the arguments for me? Is it not possible to do or am I just missing something?


----------



## phyro (Feb 8, 2012)

ie:

[cmd=]awk | tee -a /User/Desktop/test.txt[/cmd]

shows


```
usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
```

The  text.txt file is empty because nothing happened, it just displayed the system input.

Whereas

[cmd=]date | tee -a /User/Desktop/test.txt[/cmd]

writes


```
Tue  7 Feb 2012 20:31:31 PST
```

to test.txt because there's a default output of the date.

I need to make it write the arguments of the empty command to the textfile 

So the text file looks like

[cmd=]awk | tee -a /User/Desktop/test.txt[/cmd]

writes 


```
usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
```

and 

[cmd=]date | tee -a /User/Desktop/test.txt[/cmd]

writes the date.


----------



## J65nko (Feb 8, 2012)

```
$ awk
usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
$ awk >logfile
usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
$ cat logfile
$ awk >logfile 2>&1
$ cat logfile
usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
```


----------



## phyro (Feb 8, 2012)

SCORE!!

Thank you very much, that works like a charm


----------



## J65nko (Feb 8, 2012)

If you add that *exec >logfile 2>&1*, that I mentioned before, as the first line in your script, you don't have to repeat the *>logfile 2>&1* after each individual command.


----------

