LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-04-2008, 01:39 PM   #1
Acidg3rm5
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Rep: Reputation: 0
getting the status of grep


hi, i have the following codes, which prompt my user to change the item's description.

I want to be able to tell user that no such item exist if the search returns nothing. How to i do it?

echo "Please enter the item description you wish to inquire"
read itemDesc
#use grep to search. -i to ignore case, -w to match the string.
grep -iw "$itemDesc" sales.txt
#need a if else statment here to get status of grep. echo "No such item #found if grep has no results"
echo "Press enter to return to main menu"
 
Old 11-04-2008, 02:05 PM   #2
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
You will find all the scripting help you will need here - http://tldp.org/LDP/abs/html/

Enjoy!


Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 04:57 AM.
 
Old 11-04-2008, 02:21 PM   #3
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Linux Mint 17, Debian 8
Posts: 7,854

Rep: Reputation: 310Reputation: 310Reputation: 310Reputation: 310
You need to check the return code from the grep command. You find the return code from the most recent command in the $? variable i.e.:

grep -iw "$itemDesc" sales.txt
if test "$?" != 0;
then

----------------------
Steve Stites
 
Old 11-04-2008, 06:01 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
I prefer the numeric test
Code:
if [[ $? -ne 0 ]]
then
    echo "not found"
fi
 
Old 11-05-2008, 08:36 AM   #5
Acidg3rm5
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
I prefer the numeric test
Code:
if [[ $? -ne 0 ]]
then
    echo "not found"
fi
Thanks. it worked. Anyway, am trying to put a nice output after i search. but i'm hitting an error.

Code:
search=`grep -iw "$itemDate" sales.txt` 

if [[ $? -ne 0 ]]
then
    echo "Item not found."

else

    echo $search | awk 'BEGIN{ FS=":" print "Description\t \Unit Price\t \tQty\t Date"} {printf("%s\t%s\t   $%.2f\n", NR,$4,total)}'
fi
I would actually want to display the items when found to something like this:

Description Unit Price Qty Date
Coke 1 20 01Nov08
Chocolate 3 100 01Nov08

Anyone can give me some clues what went wrong with my AWK?

Last edited by Acidg3rm5; 11-05-2008 at 12:36 PM.
 
Old 11-05-2008, 10:38 AM   #6
Acidg3rm5
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thought that i should not start a new thread to ask for help. got another problem in my program
Quote:
cat -b sales.txt
echo "Please enter the number you wish to delete: "
read choice
sed -i "$choice d" sales.txt

#need a if statement here. if sed -i "$choice d" sales.txt is invalid, i want to print an error message that tells user he selected wrong #choice.

#else. it prints the below
echo "Transaction No. $choice deleted!"


echo "Press enter to return to main menu"
read
;;
 
Old 11-05-2008, 06:14 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Define 'invalid' ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
hda:status timeout:status = 0xd0 {Busy} dahlinkj Linux - Newbie 2 05-27-2008 05:36 PM
How to check grep status when find elements return 0 ahjiefreak Programming 18 12-18-2007 05:06 PM
Installing RedHat 8.0 onto Dell PowerEdge SC1425 - hdc: status error: status = 0x58 fishsponge Linux - General 0 07-11-2006 09:02 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM


All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration