|
|
|
|
|
|
| Author |
Message |
martin *nix forums beginner
Joined: 22 Feb 2005
Posts: 44
|
Posted: Fri Feb 18, 2005 10:38 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
Thank you to everyone who has helped me understand this!
This has been a great intro to unix and I really appreciate the unix
community
for being so helpful, patient, and friendly.
I finally finished that little script and it was brilliant seeing it
zoom away flawlessly after so much experimentation and trial.
Sincerely thank you all,
Martin |
|
| Back to top |
|
 |
Ed Morton *nix forums Guru
Joined: 20 Feb 2005
Posts: 1073
|
Posted: Thu Feb 17, 2005 1:55 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
martin wrote:
| Quote: | ok! from the command line, after typing either "sh" or "bash" i'm able
to get the following to successfully run: if grep "No warnings"
output1.txt; then echo y; else echo n; fi
but, remember that i'm trying to build these commands into an
applescript that i have saved as a droplet...
|
applescipts, droplets, blast-ended skrewts, whatever....
The thing that's pertinent is that you have a file containg a shell
script (right?) and you'd like to execute it under a bourne-like shell
because csh-like shells are poor for scripting (see question 19 in the
FAQ, http://home.comcast.net/~j.p.h/cus-faq.html#S) so put a shebang at
the top of your shell script, e.g.:
#!/usr/bin/sh
so the remainder of the file gets run by that interpreter. Use bash or
sh or ksh with the appropriate path.
Ed. |
|
| Back to top |
|
 |
martin *nix forums beginner
Joined: 22 Feb 2005
Posts: 44
|
Posted: Wed Feb 16, 2005 9:40 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
ok! from the command line, after typing either "sh" or "bash" i'm able
to get the following to successfully run: if grep "No warnings"
output1.txt; then echo y; else echo n; fi
but, remember that i'm trying to build these commands into an
applescript that i have saved as a droplet... so i'm testing by using
the droplet - and when i put the this command --
sh; if grep "No warnings" outfile.txt; then echo y; else echo n; fi
or,
bash; if grep "No warnings" outfile.txt; then echo y; else echo n; fi
into applescript to be sent to terminal upon droplet activation... a
strange thing happens... terminal allows the switch to either sh or
bash - but then stops. the rest of the command line doesn't even
appear in the terminal window. i tried a similar thing with the
following:
sh; ./alan 3build6.alan > outfile.txt
which froze after "sh" as well. i checked in the working directory for
the outfile.txt and didn't see it or the executable build file that
should have been generated... (i even refreshed the window view and
tried closing/reopening it just to make sure, but it still wasn't
there.) - until - funny enough, i thought to go back to terminal and
type "exit" to exit the sh shell. that put me back at the tcsh prompt
- and the outfile.txt and exe file both appeared in the working dir.
so, i went back, thinking that i had to exit bash each time... and
here's what i tried:
sh; ./alan 3build6.alan > outfile.txt; exit
but that didn't work. i tried about a dozen other varriants of this,
working with switching back and forth from tcsh to sh... thinking that
i needed to do that to trigger things to recognize the results of
commands executed while in sh. nothing worked. (note: most of my
testing was done by writing the commands into the applescript droplet
and sending them to terminal that way)
but i also tried typing directly into terminal for testing purposes -
doing the simpler "./alan 3build6.alan >outfile.txt" at the sh and
bash prompts - and that worked. as did: "if grep "No warnings"
outfile.txt; then echo y; else echo n; fi "
and this: if [ -f "3build6.a3c" ]; then ./arun 3build6.a3c; else echo
n; fi -
which worked! but only when i type it directly into the prompt - not
if i send it via applescript - but take my word for it - applescript
*can send script commands to terminal - i'm doing it for a lot of the
commands already - and i have a feeling that the tricky part here is
with getting the command to continue after switching to bash or sh...
and then (if i should be) exiting the sh or bash to tcsh.
i hope i'm making sense - because i feel like i'm only a hair away from
getting all of this working! - thanks to you folks!...
just, if you have thoughts about the switching between shells at the
prompts - and what i might need to do to keep the commands flowing,
please advise.
thank you
martin
oh, one more thing that i tested at the bash prompt:
if [ -f "3build6.a3c" ]; then ./arun bogus_file.a3c; else echo n; fi
and
if [ -f "3build6.a3c" ]; then ./arun bogus_file.a3c; else open
outfile.txt; fi
neither of which actually trigger the else portion... though, both fail
since there is no bogus file - but the compiler (through terminal) is
still trying to execute a file called "bogus_file.a3c" ... and that
triggers a bunch of text to print to terminal screen saying:
"SYSTEM ERROR: Can't open adventure code file 'bogus_file.a3c'."
but it's very close to all coming together now !!!
M |
|
| Back to top |
|
 |
Kenny McCormack *nix forums Guru
Joined: 24 Mar 2005
Posts: 657
|
Posted: Wed Feb 16, 2005 2:51 am Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
In article <4212B57D.4090902@pcisys.net>,
Bill Seivert <seivert@pcisys.net> wrote:
| Quote: |
Kenny McCormack wrote:
In article <1108500165.874722.228770@f14g2000cwb.googlegroups.com>,
martin <ultragrimsocialmen@yahoo.com> wrote:
...
so, what could be wrong with my environment? my computer is a mac with
os x 2.8 - and i'm working with darwin version 6.8.
I think the default shell in MacOSX is tcsh.
Not that there is anything wrong with that - but most of the examples
posted here assume a Bourne-ish shell.
Just type sh to go into the Bourne shell on the PowerBook.
|
Or not. I.e., just realize that that's why the examples posted here don't
work. |
|
| Back to top |
|
 |
Bill Seivert *nix forums Guru Wannabe
Joined: 06 Mar 2005
Posts: 159
|
Posted: Wed Feb 16, 2005 1:52 am Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
Kenny McCormack wrote:
| Quote: | In article <1108500165.874722.228770@f14g2000cwb.googlegroups.com>,
martin <ultragrimsocialmen@yahoo.com> wrote:
...
so, what could be wrong with my environment? my computer is a mac with
os x 2.8 - and i'm working with darwin version 6.8.
I think the default shell in MacOSX is tcsh.
Not that there is anything wrong with that - but most of the examples
posted here assume a Bourne-ish shell.
|
Just type sh to go into the Bourne shell on the PowerBook. |
|
| Back to top |
|
 |
Kenny McCormack *nix forums Guru
Joined: 24 Mar 2005
Posts: 657
|
Posted: Tue Feb 15, 2005 8:21 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
In article <1108500165.874722.228770@f14g2000cwb.googlegroups.com>,
martin <ultragrimsocialmen@yahoo.com> wrote:
....
| Quote: | so, what could be wrong with my environment? my computer is a mac with
os x 2.8 - and i'm working with darwin version 6.8.
|
I think the default shell in MacOSX is tcsh.
Not that there is anything wrong with that - but most of the examples
posted here assume a Bourne-ish shell. |
|
| Back to top |
|
 |
martin *nix forums beginner
Joined: 22 Feb 2005
Posts: 44
|
Posted: Tue Feb 15, 2005 7:42 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
Ed wrote:
"Do this:
a) create a file named "output1.txt" that contains only the string "No
warnings".
b) create a file named "output2.txt" that contains only the string
"Many
warnings".
c) execute this command:
if grep "No warnings" output1.txt; then echo y; else echo n; fi
d) execute this command:
if grep "No warnings" output2.txt; then echo y; else echo n; fi
"
ok. i copied and pasted your text to terminal after making the two
files and i got the "if: Expression Syntax." message.
even though this hasn't been working - i have been able to execute most
simple commands ( as i'm working through unix for mac os x visual
quickpro guide book) without any problems.
so, what could be wrong with my environment? my computer is a mac with
os x 2.8 - and i'm working with darwin version 6.8.
Martin |
|
| Back to top |
|
 |
Ed Morton *nix forums Guru
Joined: 20 Feb 2005
Posts: 1073
|
Posted: Mon Feb 14, 2005 10:25 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
martin wrote:
| Quote: | "I suggested using grep
instead as it better fits how you're trying to use awk. Is the problem
that you don't have access to grep or is there some othe reason you're
focussing on awk? "
i tried using grep too... but that turned up the same results - ie, it
seemed to detect the presence of the "no warnings" string in the output
file, and would go on to compile the executable - but that only worked
when the file compiled correctly. if the "no warnings" string wasn't
present, the ELSE portion of the statement wouldn't trigger. i think
that was due to there not really being a test condition being set up
with grep.
|
Do this:
a) create a file named "output1.txt" that contains only the string "No
warnings".
b) create a file named "output2.txt" that contains only the string "Many
warnings".
c) execute this command:
if grep "No warnings" output1.txt; then echo y; else echo n; fi
d) execute this command:
if grep "No warnings" output2.txt; then echo y; else echo n; fi
The output from "c" should be:
No warnings
y
and the output from "d" should be:
n
If it is, then there's something about your real "output.txt" file
that's causing your problems. If it isn't then there's something about
your environment that's causing your problems.
| Quote: | but here's what i tried:
if grep "No warnings" output.txt > /dev/null
then
./arun 3build6
else
open output.txt
fi
it failed with /dev/null -( remember i asked about that )- so i took
the /dev/null out and it still failed to trigger the else clause.
|
The above should work. Without "> /dev/null" you'd just additionally see
the result of the grep.
| Quote: | instead, terminal proceeds as if it's trying to issue the "./arun
......." command even if the file isn't an executable file. and that
results in terminal giving the default message for a bad compile
(that's, default msg for the text-based gamming world anyway which
says something like "You wake to discover that your adventure game did
not execute properly" -- which is a confusing message that i'd like to
avoid the user having to see.
so i re-wrote it, trying to pipe the input on the first line to trigger
the condition with:
if grep ~"No warnings or errors detected" | output.txt;
|
The above is meaningless. Or, more precisely, it has a meaning that has
nothing to do with what you're trying to do.
| Quote: | etc...
... which seems like a forced part of the condition - i have to test
that the text is present in that file before going on... ( ...do you
agree?)
|
No. It's grepping for a string starting with "~" in stdin then piping
the result to your, presumably, non-executable output file.
| Quote: | then again, i realized that i might be able to test for the presence of
a file with a known name in the working directory... (i posted about
that on this ng too)
but, the "test" command hasn't jelled for me either. i'm sure it's
just because i'm not seeing how to express it with the proper syntax.
|
Simplify your code and build up. Start with:
if [ -f "file" ]
then
echo y
else
echo n
fi
Regards,
Ed. |
|
| Back to top |
|
 |
martin *nix forums beginner
Joined: 22 Feb 2005
Posts: 44
|
Posted: Mon Feb 14, 2005 9:44 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
"I suggested using grep
instead as it better fits how you're trying to use awk. Is the problem
that you don't have access to grep or is there some othe reason you're
focussing on awk? "
i tried using grep too... but that turned up the same results - ie, it
seemed to detect the presence of the "no warnings" string in the output
file, and would go on to compile the executable - but that only worked
when the file compiled correctly. if the "no warnings" string wasn't
present, the ELSE portion of the statement wouldn't trigger. i think
that was due to there not really being a test condition being set up
with grep.
but here's what i tried:
if grep "No warnings" output.txt > /dev/null
then
../arun 3build6
else
open output.txt
fi
it failed with /dev/null -( remember i asked about that )- so i took
the /dev/null out and it still failed to trigger the else clause.
instead, terminal proceeds as if it's trying to issue the "./arun
........" command even if the file isn't an executable file. and that
results in terminal giving the default message for a bad compile
(that's, default msg for the text-based gamming world anyway which
says something like "You wake to discover that your adventure game did
not execute properly" -- which is a confusing message that i'd like to
avoid the user having to see.
so i re-wrote it, trying to pipe the input on the first line to trigger
the condition with:
if grep ~"No warnings or errors detected" | output.txt;
etc...
.... which seems like a forced part of the condition - i have to test
that the text is present in that file before going on... ( ...do you
agree?)
then again, i realized that i might be able to test for the presence of
a file with a known name in the working directory... (i posted about
that on this ng too)
but, the "test" command hasn't jelled for me either. i'm sure it's
just because i'm not seeing how to express it with the proper syntax.
thanks again
Martin |
|
| Back to top |
|
 |
Ed Morton *nix forums Guru
Joined: 20 Feb 2005
Posts: 1073
|
Posted: Mon Feb 14, 2005 9:24 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
martin wrote:
| Quote: | Bill Marcum wrote:
"Awk commands should either be quoted or written in a separate file to
be executed with "awk -f". In your script, awk would see "if" as a
command, ~"No errors detected" as a filename, and the shell interprets
the rest of the script. This should cause an error because the shell
sees an else with no if. "
wow.
understanding the man pages is tough enough...but, I thought that the
following (quoted from the man awk page)
"if( expression ) statement [ else statement ]
Statements are terminated by semicolons, newlines or right braces."
meant that an if statement could be written on one line if seperated by
semicolons...
But, are you saying that if i put the original code in a seperate file
(does file extension matter? ie, .txt, .sh ??) and then had as the
command line statement
"awk -f filename.extension"
that's going to call the file with the code:
~"No errors detected" | ouput.txt
./arun filename.a3c
else
open output.txt
fi
and all of that will execute automatically, running down the list
checking to see if each condition is met or not?
|
No, he isn't saying that at all. He's sayign that an awk script EITHER
has to be put within quotes OR put in a file and executed with "-f".
That's all true, but what you have here is not awk.
| Quote: | I was avoiding doing it that way mostly because i'm new and that seemed
more complicated... but if you say it sounds like it'll work - i'll do
what i need to do to get applescript to write that code from variables
- save it as a file, and call the file with the awk -f flag.
|
What you have written is NOT an awk script. It's a shell script trying
to call awk to search for a pattern in a file. I suggested using grep
instead as it better fits how you're trying to use awk. Is the problem
that you don't have access to grep or is there some othe reason you're
focussing on awk?
Ed.
|
|
| Back to top |
|
 |
martin *nix forums beginner
Joined: 22 Feb 2005
Posts: 44
|
Posted: Mon Feb 14, 2005 8:31 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
Bill Marcum wrote:
"Awk commands should either be quoted or written in a separate file to
be executed with "awk -f". In your script, awk would see "if" as a
command, ~"No errors detected" as a filename, and the shell interprets
the rest of the script. This should cause an error because the shell
sees an else with no if. "
wow.
understanding the man pages is tough enough...but, I thought that the
following (quoted from the man awk page)
"if( expression ) statement [ else statement ]
Statements are terminated by semicolons, newlines or right braces."
meant that an if statement could be written on one line if seperated by
semicolons...
But, are you saying that if i put the original code in a seperate file
(does file extension matter? ie, .txt, .sh ??) and then had as the
command line statement
"awk -f filename.extension"
that's going to call the file with the code:
~"No errors detected" | ouput.txt
./arun filename.a3c
else
open output.txt
fi
and all of that will execute automatically, running down the list
checking to see if each condition is met or not?
I was avoiding doing it that way mostly because i'm new and that seemed
more complicated... but if you say it sounds like it'll work - i'll do
what i need to do to get applescript to write that code from variables
- save it as a file, and call the file with the awk -f flag.
thank you
Martin |
|
| Back to top |
|
 |
martin *nix forums beginner
Joined: 22 Feb 2005
Posts: 44
|
Posted: Mon Feb 14, 2005 8:18 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
| Quote: | Seems like we just answered this one recently.... Regards,
Ed. |
I know. I'm sorry - but it just wasn't working no matter what i tried -
and i thought that maybe rephrasing my question might help make it
clearer.
Martin |
|
| Back to top |
|
 |
Bill Marcum *nix forums Guru
Joined: 28 Mar 2005
Posts: 1264
|
Posted: Sat Feb 12, 2005 9:51 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
On 12 Feb 2005 11:39:20 -0800, martin
<ultragrimsocialmen@yahoo.com> wrote:
| Quote: | awk if ~"No errors detected" | ouput.txt
./arun filename.a3c
else
open output.txt
fi
Awk commands should either be quoted or written in a separate file to |
be executed with "awk -f". In your script, awk would see "if" as a
command, ~"No errors detected" as a filename, and the shell interprets
the rest of the script. This should cause an error because the shell
sees an else with no if. |
|
| Back to top |
|
 |
Ed Morton *nix forums Guru
Joined: 20 Feb 2005
Posts: 1073
|
Posted: Sat Feb 12, 2005 7:00 pm Post subject:
Re: why won't my awk if/else work for both conditions?
|
|
|
martin wrote:
| Quote: | awk if ~"No errors detected" | ouput.txt
|
Awk doesn't return succ/fail based on whether or not it finds a pattern,
and even if it did that's the wrong syntax.
Try this:
if grep "No errors detected" output.txt
| Quote: | ./arun filename.a3c
else
open output.txt
fi
|
and look at the grep man page for which options you want based on what
you want to see on stdout. Seems like we just answered this one recently....
Regards,
Ed. |
|
| Back to top |
|
 |
martin *nix forums beginner
Joined: 22 Feb 2005
Posts: 44
|
Posted: Sat Feb 12, 2005 6:39 pm Post subject:
why won't my awk if/else work for both conditions?
|
|
|
awk if ~"No errors detected" | ouput.txt
../arun filename.a3c
else
open output.txt
fi
I need some assistance in figuring out why this if/else only partly
works. it works fine when the file i'm compiling is a success. in
that case, line one catches the "no errors" text and line two flows
smoothly to issue a command to execute the compiled program. BUT, if
my source code file Fails and does not have the text "No errors
detected" the ELSE command to just open the output.txt file does NOT
work. instead, it appears that terminal has tried to execute a badly
compiled file based on line two - tries to run it in the terminal
window, which fails.
when i type the command "open output.txt" on the command line it opens
the file correctly with textedit. but when i try to run the code above
it doesn't work.
why? and what have i done wrong?
thank you
martin |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Jan 08, 2009 10:18 pm | All times are GMT
|
|
Credit Cards | Mobile Phones | Online Advertising | Bad Credit Mortgages | Debt Consolidation
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|