bash script

#1 What does the below line do ?
echo `date`" "`cat $LOG` >> $LOG.history

#2 What does gawk '$7 ~ /&meg=/ {print}' do ? What's the different between awk & gawk ?
For $7, does it extract column 7, delimited by space
AXISHKAsked:
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.

woolmilkporcCommented:
The first command appends the current date followed by a space plus the content of a file whose name is stored in the variable "$LOG" to a file with the same name as in "$LOG" with ".history" appended.

The awk snippet checks whether the 7th space- or tab-delimited field of the current input line contains "&meg=" and if so it prints the whole line containing that 7th field.

There are several implementations of "awk". "gawk" is "GNU awk". On LInux systems it's in most cases the same as "awk".
0
AXISHKAuthor Commented:
Tks.

For the first command, does it mean a date and a space will be append at the begining of each line in$LOG file ?

For second command, the whole line will be extracted, rather than the 7th column ?
0
woolmilkporcCommented:
1) Taking the command as you posted it the date and the space appears only once at the beginning of the new content which is appended to $LOG.history. `cat $LOG` is not enclosed in quotes, however, so all the content of the file appears in one single line,  so that at the end each line is prefixed with the date.

2) Yes, the whole line will be printed out if its 7th field matches your criterion.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

woolmilkporcCommented:
My explanation of the first command might be a bit ambiguous, so let's make an example.

Assume the file whose name is in $LOG contains this:

1
2
3

and $LOG.history contains initially nothing.

After running your command as posted $LOG.history will  contain:

Fri Jul 3 11:35:21 DFT 2015 1 2 3

At the next run, with $LOG containing

4
5
6

you will see in $LOG.history:

Fri Jul 3 11:35:21 DFT 2015 1 2 3
Fri Jul 3 11:35:48 DFT 2015 4 5 6

and so on.

Taking the same file content but modifying the command like this:

echo `date`" ""`cat $LOG`" >> $LOG.history

the result in $LOG.history will be after the first run

Fri Jul 3 11:35:21 DFT 2015 1
2
3

and after the second run:

Fri Jul 3 11:35:21 DFT 2015 1
2
3
Fri Jul 3 11:35:48 DFT 2015 4
5
6

Hope this makes things a bit clearer.
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
AXISHKAuthor Commented:
Great Tks.

echo `date`" ""`cat $LOG`" >> $LOG.history

What make it difference when adding " " between `cat $LOG`.

Tks again.
0
woolmilkporcCommented:
"echo" turns several consecutive spaces into one, and, important in this case, removes the line feeds between the strings to be echoed.

To avoid this enclose the data to be echoed in double quotes.
The quotes turn consecutive strings (possibly separated by line feeds) into one single string which is left untouched by "echo".

Again, the example:

$LOG contains

1
2
3

i.e. 3 strings separated by line feeds, and

`cat $LOG`

represents these three strings and their line feeds:

1
2
3

But "echo" in echo `cat $LOG` removes the line feeds, so the result of

echo `cat $LOG`

is

1 2 3

"`cat $LOG`"  (note the quotes!) on the other hand represents just one single string with embedded line feeds.

"echo" does not touch content between quotes, so the result of

echo "`cat $LOG`"

is

1
2
3

i. e. the original content (three strings separated by line feeds).

You might want to ask why we don't use single quotes (' ') ?
Now, that's because the shell itself does not touch content between single quotes,
so this $LOG variable will not be expanded to the corresponding value but will be treated literally as '$LOG' which is obviously not desired here.
0
AXISHKAuthor Commented:
Tks
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.