Assistance with bash, include command in output to a file

Hi Team,

   I just need to create a simple  script that captures some commands and output to a file. In my code below, it is just sending the actual outputs and not the command itself, making it difficult to note the start and end output stream of each command.  In the example below, I just have 5 shell commands....in the final script, there will be over 50.

[root@testing ~]# cat baseline.sh
#!/bin/bash
BASEFILE=BASELINE_$(date '+%d_%b_%Y_%H_%M_%S').txt
uptime >> $BASEFILE 2>&1 
who >> $BASEFILE 2>&1
last -n 100 >> $BASEFILE 2>&1
free -m  >> $BASEFILE 2>&1
df -h  >> $BASEFILE 2>&1

Open in new window


The file just shows the raw output of the individual commands, and not the commands that invoked them, making it difficult to find the start and end of each output block.

[root@testing ~]# cat BASELINE_26_Apr_2015_09_43_46.txt
 09:43:46 up  1:16,  4 users,  load average: 0.00, 0.00, 0.00
au157 tty1         2015-04-26 18:47 (:0)
au157 pts/0        2015-04-26 18:47 (:0.0)
au157 pts/1        2015-04-26 08:48 (192.168.76.1)
au157pts/2        2015-04-26 09:01 (192.168.76.1)

wtmp begins Sun Apr 26 09:39:02 2015
             total       used       free     shared    buffers     cached
Mem:          1894        618       1275          4         60        325
-/+ buffers/cache:        232       1662
Swap:         4063          0       4063
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_k2dot0-lv_root
                       35G  4.1G   30G  13% /
tmpfs                 948M   80K  948M   1% /dev/shm
/dev/sda1             477M   68M  385M  15% /boot

Open in new window


Thanks very much.
rleyba828Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
#!/bin/bash
BASEFILE=BASELINE_$(date '+%d_%b_%Y_%H_%M_%S').txt
exec >> $BASEFILE 2>&1
set -x
uptime
who
last -n 100
free -m
df -h
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rleyba828Author Commented:
Yes, this second variation works well.   thanks very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.