|
Home > Archive > Unix Programming > March 2007 > help with shell scripting!!
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 |
help with shell scripting!!
|
|
|
| Hi all,
I am new to shell programming, I want to do a string comparison, I am
using tcsh here is the script I have
written,
#!/usr/local/bin/tcsh -f
set y=" full of errors"
if [`echo $y | grep errors`]
then
echo " errors found"
fi
It gives error with if: Expression Syntax. Can some body explain what
is the problem here?
Thanks in advance,
| |
| John Gordon 2007-03-26, 1:21 pm |
| In <1174912249.410129.74590@d57g2000hsg.googlegroups.com> "seema" <seema_coma@yahoo.co.in> writes:
> It gives error with if: Expression Syntax. Can some body explain what
> is the problem here?
There are lots of different shells you can use for shell programming.
Unfortunately, they're all different, and have different syntax.
You seem to be using Bourne shell syntax, but you're executing the script
with tcsh, and the two don't mix.
So, you need to pick one or the other.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
| |
| Chris F.A. Johnson 2007-03-26, 7:32 pm |
| On 2007-03-26, seema wrote:
> Hi all,
>
> I am new to shell programming, I want to do a string comparison, I am
> using tcsh here is the script I have
> written,
>
> #!/usr/local/bin/tcsh -f
The csh (including tcsh) is not recommended for scripting:
<http://www.grymoire.com/Unix/CshTop10.txt>
<http://www.grymoire.com/Unix/Csh.html#uh-0>
<http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>
> set y=" full of errors"
Bourne syntax:
y=" full of errors"
> if [`echo $y | grep errors`]
That is not csh syntax, and will not work in a Bourne-type shell
(you need spaces around '[' and ']').
> then
> echo " errors found"
> fi
> It gives error with if: Expression Syntax. Can some body explain what
> is the problem here?
#!/bin/sh
y=" full of errors"
case $y in
*errors*) echo " errors found" ;;
esac
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| SM Ryan 2007-03-26, 7:32 pm |
| "seema" <seema_coma@yahoo.co.in> wrote:
# Hi all,
#
# I am new to shell programming, I want to do a string comparison, I am
# using tcsh here is the script I have
# written,
#
# #!/usr/local/bin/tcsh -f
#
# set y=" full of errors"
#
# if [`echo $y | grep errors`]
What you're doing here is probably
echo $y | grep errors
which write ' full of errors' to stdout and then insert stdout into the string
[ full of errors]
which becomes a call to 'test' with arguments 'full' 'of' 'errors]'
which is not any expression the test command recognizes.
You need to give arguments the test command will recognises,
such -n string, properly quote the arguments, and you should space
the brackets. In bourne shell it would look like
if [ -n "`echo $y | grep errors`" ]
then
echo "errors found"
fi
--
SM Ryan http://www.rawbw.com/~wyrmwif/
God's a skeeball fanatic.
| |
| Logan Shaw 2007-03-27, 1:18 am |
| SM Ryan wrote:
> You need to give arguments the test command will recognises,
> such -n string, properly quote the arguments, and you should space
> the brackets. In bourne shell it would look like
> if [ -n "`echo $y | grep errors`" ]
> then
> echo "errors found"
> fi
Or, bypass the test operation completely and use the exit code of
grep to indicate whether the string was found:
if echo "$y" | grep errors > /dev/null
then
echo "errors found"
fi
Or use the expr command:
if expr "$y" : '.*errors' > /dev/null
then
echo "errors found"
fi
Or, as someone else said, use a case statement (the most efficient way).
- Logan
|
|
|
|
|