 |
 |
Seeking expert help with "wget | awk" part in shellscript
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2003
Location: Oslo, Norway
Status:
Offline
|
|
Hi guys.
I need help setting up a command in my shellscript (used to build my G4 optimized Firefox builds).
What I want is the shellscript to check if tinderbox is green before it proceed.
I have received the following lines from a GNU/Linux builder, but I cant get it to work correctly...
Code:
# Tinderbox check. Set tree accordingly (SeaMonkey, Firefox, Thunderbird, etc). If you're using something other than Linux, alter accordingly.
function checktree()
{
wget --proxy=off -qO- "http://tinderbox.mozilla.org/showbuilds.cgi?tree=${tree}&quickparse=1" | awk 'BEGIN {FS="|"} $1=="Build" && $3~/Linux/ && $4!="success" { exit 1 }'
}
I want it to go something like this:
Code:
if [ `wget --proxy=off -qO- "http://tinderbox.mozilla.org/showbuilds.cgi?tree=Firefox&quickparse=1" | awk 'BEGIN {FS="|"} $1=="Build" && $3~/MacOSX/ && $4!="success" { exit 1 }'` = "success" ]; then
** Run whatever commands I place here**
fi
Meaning. The output from this command should tell it what to do:
Code:
wget --proxy=off -qO- "http://tinderbox.mozilla.org/showbuilds.cgi?tree=Firefox&quickparse=1"
If it at the end of the lines behind the two MacOSX appearances say "success", then proceed.
The first part of the script (before the pipe is ok), but what I really dont get is the awk part. If it say "testfailed" I dont want it to run the rest of the script...
Any "Guru's" out there?
Thanks in advance!
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
The awk command returns an exit status of either true (0) or false (1), depending on whether the input (wget's output) contained what you're making it look for.
Here's a breakdown of the awk command: - {FS="|"} means that the field separator is |
- $1=="Build" means that the first field, as separated by the FS, is the string "Build"
- $3~/MacOSX/ means that the third field contains a match for the regex expression inside the //. In this case it looks for "MacOSX."
- $4!="success" means the fourth field is NOT "success."
If all of these conditions are met, i.e. if the MacOSX build did NOT succeed, the command exits with 1, which is false. So instead of doing
Code:
if [ "`the above command`" = "success" ]; then
(Note that if you were to do this, you should quote the `command`, since it's to be interpreted as a string)
You should do (corrected):
Code:
if (the above command); then
If you want to do it your way, you need to make awk print the fourth field. Do that with
Code:
awk 'BEGIN {FS="|"} $1=="Build" && $3~/MacOSX/ { print $4 }'
As is, this currently returns two lines of "success," so you should make the regex defining field three more specific. For instance, you could specify only the "Build|Firefox|MacOSX Darwin 7.7.0 imola Depend|success" line by doing
Code:
awk 'BEGIN {FS="|"} $1=="Build" && $3~/MacOSX.*7\.7\.0/ { print $4 }'
Incidentally, the best guide I've ever found for bash scripting is Mendel Cooper's Advanced Bash-Scripting Guide. It's absolutely invaluable.
(Last edited by wataru; Apr 22, 2005 at 03:57 AM.
(Reason:Code correction))
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2003
Location: Oslo, Norway
Status:
Offline
|
|
Wow!
Thanks a lot for the fast and understandable reply.
I've now got a working command, just the way I want it:
Code:
if [ `wget --proxy=off -qO- "http://tinderbox.mozilla.org/showbuilds.cgi?tree=Firefox&quickparse=1" | awk 'BEGIN {FS="|"} $1=="Build" && $3~/MacOSX.*7\.7\.0/ { print $4 }'` = "success" ]; then
** my commands here **
fi
Now off to implement the code in my shellscript...
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
You should surround the `command` with double-quotes. In this case it's not technically necessary, but it's good practice. (It's necessary when the result of `command` is a string with spaces or other "forbidden" characters in it, or if it's null.)
(Last edited by wataru; Apr 22, 2005 at 03:53 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2003
Location: Oslo, Norway
Status:
Offline
|
|
Originally posted by wataru:
You should surround the `command` with double-quotes.
Done! 
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
Incidentally, I was wrong about the syntax of the "good" way, which is probably why you decided to do it your way instead.
It should be
Code:
if (wget --proxy=off -qO- "http://tinderbox.mozilla.org/showbuilds.cgi?tree=${tree}&quickparse=1" | awk 'BEGIN {FS="|"} $1=="Build" && $3~/Linux/ && $4!="success" { exit 1 }'); then
Using () evaluates the truth of the command's exit code. Using [] just evaluates the exit code as a string, which is always true.
I say that this is the "good" way because to me it seems cleaner than having the command return a string and checking that it equals some other string. But it's entirely up to you, as they both work.
Also, I'd like to thank you for starting this thread. It wasn't until I tried to decypher your command that I really started to understand how to use awk. Now I'm going through my scripts and replacing tons of pipes through grep and cut that can be done with a single awk command.
(Last edited by wataru; Apr 18, 2005 at 12:26 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2003
Location: Oslo, Norway
Status:
Offline
|
|
Thanks for the feedback wataru.
I'll check out your fixed command, and see if it makes more sense in my script.
Its a good think that your investigation in awk commands for me actually give you something usefull back! 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|