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" |
You will find all the scripting help you will need here - http://tldp.org/LDP/abs/html/
Enjoy! :) Linux Archive |
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 |
I prefer the numeric test
Code:
if [[ $? -ne 0 ]] |
Quote:
Code:
search=`grep -iw "$itemDate" sales.txt` 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? |
Thought that i should not start a new thread to ask for help. got another problem in my program
Quote:
|
Define 'invalid' ?
|
All times are GMT -5. The time now is 09:38 PM. |