help with shell scripting!!
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > help with shell scripting!!




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    help with shell scripting!!  
seema


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-26-07 01:20 PM

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,






[ Post a follow-up to this message ]



    Re: help with shell scripting!!  
John Gordon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-26-07 06:21 PM

In <1174912249.410129.74590@d57g2000hsg.googlegroups.com> "seema" <seema_com
a@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"






[ Post a follow-up to this message ]



    Re: help with shell scripting!!  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-27-07 12:32 AM

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





[ Post a follow-up to this message ]



    Re: help with shell scripting!!  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-27-07 12:32 AM

"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 stri
ng
[ 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.





[ Post a follow-up to this message ]



    Re: help with shell scripting!!  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-27-07 06: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 07:42 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

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
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register