trying to make a script to capture command output

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
Code:
awk | tee -a /Desktop/test.txt | echo "******** AWK ********" >> /Users/name/Desktop/test.txt

text.txt looks like:
Code:
******** AWK ********
when you type awk without arguments you get something like this..
Code:
$ 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.
 
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.

Code:
#!/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 "$*".

Code:
cat show-args                                                
#!/bin/sh

echo "My arguments or parameters are: $@"
A sample run:
Code:
$ sh show-args John Lisa Mary
My arguments or parameters are: John Lisa Mary
 
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?
 
ie:

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

shows

Code:
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

Code:
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

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

and

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

writes the date.
 
Code:
$ 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 ...]
 
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.
 
Back
Top