# Capture the command run in the log file

**URL:** <https://community.unix.com/t/capture-the-command-run-in-the-log-file/164044>\
**Category:** Shell Programming and Scripting\
**Created:** [August 8, 2006, 3:12pm UTC](https://community.unix.com/t/capture-the-command-run-in-the-log-file/164044 "2006-08-08T15:12:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmedwaseem2000](https://community.unix.com/letter_avatar/ahmedwaseem2000/32/5_5575768a8748004e209b776fc1b2916d.png) [@ahmedwaseem2000](https://community.unix.com/u/ahmedwaseem2000)\
**Post date:** [August 8, 2006, 3:12pm UTC](https://community.unix.com/t/capture-the-command-run-in-the-log-file/164044/1 "2006-08-08T15:12:56Z")

</div>

Hi ,

I have seen some log files where they have captured the command that is being executed, comments present in the scripts and the out put of the command as well, through scripts. could any one of you please let me know how do i do that?

Thanks in advance.

Cheers,  
Waseem

---

<div class="post-metadata">

**Author:** ![tmarikle](https://community.unix.com/letter_avatar/tmarikle/32/5_5575768a8748004e209b776fc1b2916d.png) [@tmarikle](https://community.unix.com/u/tmarikle)\
**Post date:** [August 8, 2006, 3:27pm UTC](https://community.unix.com/t/capture-the-command-run-in-the-log-file/164044/2 "2006-08-08T15:27:22Z")

</div>

```nohighlight
echo "yourcommand args" > log_file.txt
yourcommand args >> log_file.txt

```

or maybe it's running a trace and capturing output and tracing execution:

```nohighlight
{
    set -x
    yourcommand args
    set +x
} > yourlog.txt 2>&1

```

---

<div class="post-metadata">

**Author:** ![ahmedwaseem2000](https://community.unix.com/letter_avatar/ahmedwaseem2000/32/5_5575768a8748004e209b776fc1b2916d.png) [@ahmedwaseem2000](https://community.unix.com/u/ahmedwaseem2000)\
**Post date:** [August 9, 2006, 3:04am UTC](https://community.unix.com/t/capture-the-command-run-in-the-log-file/164044/3 "2006-08-09T03:04:58Z")

</div>

Oh- i never knew that, you could capture even the comments b tracing. i guess, you are right, as they are using set -vf probably thats how they are accomplishing this.

also, incase of command failures will the script teminate? like, in the below code, the file is not found so i want the script to terminate? if yes, then, how do i avoid from script terminating. for instance, i would want to ignore even if the file is not present and continue running the next commands?????

```nohighlight
{
set -x
rm filename
rm: could not find the file
set +x

```

i will test it later. anyways thanks for your help!!!

Cheers,  
Waseem 🙂

---

<div class="post-metadata">

**Author:** ![tmarikle](https://community.unix.com/letter_avatar/tmarikle/32/5_5575768a8748004e209b776fc1b2916d.png) [@tmarikle](https://community.unix.com/u/tmarikle)\
**Post date:** [August 9, 2006, 12:06pm UTC](https://community.unix.com/t/capture-the-command-run-in-the-log-file/164044/4 "2006-08-09T12:06:47Z")

</div>

No, the script will not terminate unless you want it to.

```nohighlight
if ! rm filename
then
    exit 1
fi

```

---

<div class="post-metadata">

**Author:** ![ahmedwaseem2000](https://community.unix.com/letter_avatar/ahmedwaseem2000/32/5_5575768a8748004e209b776fc1b2916d.png) [@ahmedwaseem2000](https://community.unix.com/u/ahmedwaseem2000)\
**Post date:** [August 9, 2006, 1:06pm UTC](https://community.unix.com/t/capture-the-command-run-in-the-log-file/164044/5 "2006-08-09T13:06:21Z")

</div>

Hey thanks for your reply.

as i do not have the access to a unix box right now, so i will try it later.

Many Thanks,

Cheers - Waseem
