Unix Shell - how to execute new command with awk - if/then/else?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > how to execute new command with awk - if/then/else?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to execute new command with awk - if/then/else?
martin

2005-02-06, 8:47 pm

i'm trying to figure out the syntax for building an awk statement that
will check the output of text in a file generated after attempting to
compile a source code file. i'm new to unix and don't know how to
issue statements to terminal if they're embedded inside an if/then
control statement.

../alan 3build6.alan > alan_output.txt ; awk (if ~/No warnings or errors
detected/ printf ./arun 3build6); else print "compile wasn't a success"

i'm trying to tell terminal to run the executable file by issuing a
command "./arun 3build6" if the string "No warnings or errors detected"
exists in the alan_output.txt file. once i understand that - i'd also
like to add some other more sophisticated commands in the "else"
portion of the code... but for now, i was just using the "compile
wasn't a success" as a way of checking the code's functionality.

i found on some of the awk pages that some examples used the printf
command - but i don't know what that does. i do now want - or do not
need for: "./arun 3build6" to be printed to the terminal screen unless
it will be printed into a command line, with the equivilant of hitting
the return button to execute it.

any wisdom would really be appreciated.

thank you.
martin

Chris F.A. Johnson

2005-02-06, 8:47 pm

On Mon, 07 Feb 2005 at 01:19 GMT, martin wrote:
> i'm trying to figure out the syntax for building an awk statement that
> will check the output of text in a file generated after attempting to
> compile a source code file. i'm new to unix and don't know how to
> issue statements to terminal if they're embedded inside an if/then
> control statement.
>
> ./alan 3build6.alan > alan_output.txt ; awk (if ~/No warnings or errors
> detected/ printf ./arun 3build6); else print "compile wasn't a success"
>
> i'm trying to tell terminal to run the executable file by issuing a
> command "./arun 3build6" if the string "No warnings or errors detected"
> exists in the alan_output.txt file. once i understand that - i'd also
> like to add some other more sophisticated commands in the "else"
> portion of the code... but for now, i was just using the "compile
> wasn't a success" as a way of checking the code's functionality.
>
> i found on some of the awk pages that some examples used the printf
> command - but i don't know what that does. i do now want - or do not
> need for: "./arun 3build6" to be printed to the terminal screen unless
> it will be printed into a command line, with the equivilant of hitting
> the return button to execute it.
>
> any wisdom would really be appreciated.


I don't know just what you are trying to do, but perhaps this will
help.

To run one command when a previous command exits successfully, and
another when it fails:

if command
then
: successful command here
else
: unsuccessful command here
fi

I you need to check for the existence of something in the first
command's output, use the success or failure of grep:

command | ## tee alan_output.txt | ## uncomment to also save output in a file
if grep "No warnings or errors detected" >/dev/null
then
: successful command here
else
: unsuccessful command here
fi


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Ed Morton

2005-02-07, 2:47 am



martin wrote:
> i'm trying to figure out the syntax for building an awk statement that
> will check the output of text in a file generated after attempting to
> compile a source code file. i'm new to unix and don't know how to
> issue statements to terminal if they're embedded inside an if/then
> control statement.
>
> ./alan 3build6.alan > alan_output.txt ; awk (if ~/No warnings or errors
> detected/ printf ./arun 3build6); else print "compile wasn't a success"


Much as I like awk, it seems inappropriate for what you appear to be
trying to do. Try this:

../alan 3build6.alan > alan_output.txt
if grep "No warnings or errors detected" alan_output.txt >/dev/null
then
./arun 3build6
else
printf "compile wasn't a success\n"
endif

If the command "allan" exits with a failure code when it issues a
warning (debatable if it should) or error (almost certainly should),
then you could simplify the above to:

if ./alan 3build6.alan > alan_output.txt
then
./arun 3build6
else
printf "compile wasn't a success\n"
endif

Regards,

Ed.
martin

2005-02-07, 7:50 am

Thanks for writing back (everyone). I'm so new to unix and terminal
that i haven't yet figured out how to do two things -(among about a
million other things
one - type multi line commands into the terminal w/out terminal wanting
to process the first line after hitting return... or setting up a code
file for terminal to fetch to collect statements to run
and, two - how to just type the code you wrote above on one line using
";" to seperate each command.

since i think it'll be easier to put it all in one line (as i'm going
to have to have applescript send that line to terminal) could you
possibly re-type what you wrote above, and show me the format for doing
it all in one command line? when i tried last night, i kept getting
errors about how the ( ) were in the wrong place - (some examples i'd
seen used curly braces and ( ) and i was trying to follow that
syntax...)

i would really appreciate being able to see both side-by-side.
thanks so much!
martin

Chris F.A. Johnson

2005-02-07, 7:50 am

On Mon, 07 Feb 2005 at 12:32 GMT, martin wrote:
> Thanks for writing back (everyone). I'm so new to unix and terminal
> that i haven't yet figured out how to do two things -(among about a
> million other things
> one - type multi line commands into the terminal w/out terminal wanting
> to process the first line after hitting return... or setting up a code
> file for terminal to fetch to collect statements to run
> and, two - how to just type the code you wrote above on one line using
> ";" to seperate each command.
>
> since i think it'll be easier to put it all in one line (as i'm going
> to have to have applescript send that line to terminal) could you
> possibly re-type what you wrote above, and show me the format for doing
> it all in one command line? when i tried last night, i kept getting
> errors about how the ( ) were in the wrong place - (some examples i'd
> seen used curly braces and ( ) and i was trying to follow that
> syntax...)
>
> i would really appreciate being able to see both side-by-side.
> thanks so much!


First, please quote relevant material.

What do you want converted to a single line? As a rule of thumb,
where there was a new line, place a semi-colon. If that doesn't
work, show us exactly what you tried (cut and paste; do not
retype) along with the exact error message, if any.

Note that braces { } need to be separated by spaces, and the
closing brace preceded by a semi-colon is it's all on one line:

{ echo A; read B; }


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
martin

2005-02-07, 8:47 pm

sorry about that... when i get home in this afternoon, i'll try it out
- and then i'll be able to cut/paste with my question again by
mid-afternoon.
thanks.
martin

martin

2005-02-07, 8:47 pm

ok, i tried to format code from your example into a single line string
of command statements...
the first time, i included the "> /dev/null/" portion, which i have no
idea what it means or causes to happen... here's the cut/paste of the
command exactly as i entered it... and, following that, the error
output from terminal.

../alan 3build6.alan >newtestoutput.txt ; if grep "No warnings"
newtestoutput.txt > /dev/null/ ; then ; ./arun 3build6 ; else ; printf
"bad compile, check file\n" ; end if ;

/dev/null/: Not a directory.

so i removed the dev/null/ portion and tried again, this time with a
different error.

../alan 3build6.alan >newtestoutput.txt; if grep "No warnings or errors
detected" newtestoutput.txt; then; ./arun 3build6; else; printf "bad
compile\n"; end if;

result: if: Expression Syntax.

note: the compile Was a success - the newtestoutput file Was
generated... but i don't believe that text string was compared in
either version before the error msg was triggered. i see what you
meant about simplifying - but in this case, i absolutly must have some
sort of string comparision to active the IF/ELSE statements.

also - if you have time, please briefly tell me what dev/null/ is for.
(btw, my computer is mac osx 2.8)
thank you
martin

Ed Morton

2005-02-07, 8:47 pm

NNTP-Posting-Host: morton-2.ih.lucent.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
X-Accept-Language: en-us, en
In-Reply-To: <1107811613.300792.134820@l41g2000cwc.googlegroups.com>
Xref: newsfeed-west.nntpserver.com comp.unix.shell:132784



martin wrote:
> ok, i tried to format code from your example into a single line string
> of command statements...
> the first time, i included the "> /dev/null/" portion, which i have no
> idea what it means or causes to happen... here's the cut/paste of the
> command exactly as i entered it... and, following that, the error
> output from terminal.
>
> ./alan 3build6.alan >newtestoutput.txt ; if grep "No warnings"
> newtestoutput.txt > /dev/null/ ; then ; ./arun 3build6 ; else ; printf
> "bad compile, check file\n" ; end if ;
>
> /dev/null/: Not a directory.


There's no "/" at the end of "/dev/null". See the original post.

> so i removed the dev/null/ portion and tried again, this time with a
> different error.
>
> ./alan 3build6.alan >newtestoutput.txt; if grep "No warnings or errors
> detected" newtestoutput.txt; then; ./arun 3build6; else; printf "bad
> compile\n"; end if;


There's no space between "end" and "if" in the original post, but it
actually should be "fi" instead.

Please use newlines and other white-space when posting code segments -
it'll make spotting problems a lot easier.

Also, include the relevant section of the post you're responding to
since it's not always obvious.

> result: if: Expression Syntax.
>
> note: the compile Was a success - the newtestoutput file Was
> generated... but i don't believe that text string was compared in
> either version before the error msg was triggered. i see what you
> meant about simplifying - but in this case, i absolutly must have some
> sort of string comparision to active the IF/ELSE statements.


Not from what you've posted so far.

> also - if you have time, please briefly tell me what dev/null/ is for.
> (btw, my computer is mac osx 2.8)


/dev/null is just a garbage can to send output to that you don't want to
see.

Ed.
martin

2005-02-08, 8:01 am

thank you! you were right - those little changes got my code a'working

sorry the statements were tough to read, but i wanted to write it on a
single line just to make sure i was doing it correctly (new to this, ya
know ;^)
thanks again
martin

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com