UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 27th, 2005, 06:03 PM
Basil_Fawlty Basil_Fawlty is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 16 Basil_Fawlty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 7 m 7 sec
Reputation Power: 0
Problem with script

Hello there

Reply With Quote
  #2  
Old April 28th, 2005, 02:04 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Quote:
Originally Posted by Basil_Fawlty
Hello there, I have a text file called directory that has the following content:

Business 103 Fuston St.
Physics 143 Nomo Ave.
Chemistry 88 Water Ave.

And I have a script named search that is supposed to print "there is a match" when it is run with a department name that is in the file, eg search Business. Likewise, it should print "no such department found" when it is run with a department name that is not in the file, eg search Earth Science. Here is what I have so far, but it is not working. The problem is with the if statment.

grep $* directory
if ???????????????
then echo there is a match
else echo no such department found
fi

I need to get this to work with grep $* directory

Thanks



Hello, try this:

:
# Test the arguments number
[ $# -eq 1 ] || {
echo "$0: call: $0 string_to_search" >&2
exit 1
}

# Test the argument against the file directory
if grep $1 directory
then echo there is a match
else echo no such department found
fi

# Decide whether "grep -i" is not a better possibility


I have tested it under AIX and under ksh. I would recommend to test in each script the total number of given arguments. It is done in the first part of my script. Then you can test argument number 1 only, but you can use your $* , too.
The grep -i would ignore the case of the argument.

Another possible construction of the if statement could be

grep ...
if [ $? -eq 0 ]
then
.....
else
....
fi

Have a fun!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Problem with script


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway