grep functions, how to test if succeeded


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep functions, how to test if succeeded
# 1  
Old 11-27-2009
grep functions, how to test if succeeded

Hello ...again.
I am stuck on this part, I have a loop with processes an operations file.
and calls different functions depending on what is in loop, which processes a database file...

Code:
#so far my add function works as intended
add()
{
    ...blah blah;
}

# delete is kinda working havn't got all bugs out yet but have tested
# and played around with grep on test files, I have been able to delete
# a line from a file, but what i need it to do is
# search database for match product/brand if no such product exist then
# display error: product: not deleted does not exist
# else remove the record. So can delete it fine, but what I'm not sure of 
# is how to do the test to see if it exist first.
delete()
{
    grep -v "${1}:${2}" $dbfile > ../tmp/tmp
    sort ../tmp/tmp > $dbfile
}

#so what i need to do is test to see if the above command happened?
# i dunno :S

#i have to do the same thing with search functions, find product and find
# brand, both need to print error if they don't exist or, in case of find 
# brand it needs to print each line that matches. which I don't think will be 
# too difficult, just unsure of how to test.

# 2  
Old 11-27-2009
check the value of $?
# 3  
Old 11-28-2009
Quote:
Originally Posted by ghostdog74
check the value of $?
do you mean after I grep the line?

I was thinking I could test the string is non-zero eg.

Code:
if [ -z (grep "${1}${2}" $dbfile) ]
then
    echo "Error:not deleted:product does not exist in database"
    exit 1
else
    grep -v "${1}${2}" $dbfile > ../tmp/tmp
    cat ../tmp/tmp > $dbfile
fi

but I'm not positive on that syntax.

---------- Post updated at 04:05 PM ---------- Previous update was at 03:08 PM ----------

anyone know how i can do this using test -z ?

i've tried putting "grep 'search' file" into a variable, and then using
of [ -z tmp ] but this still has errors, and always evaluates as true.

---------- Post updated at 04:19 PM ---------- Previous update was at 04:05 PM ----------

just realized that won't work... don't know what I was thinking.

---------- Post updated at 04:30 PM ---------- Previous update was at 04:19 PM ----------

ok figured it out in test:

xxx
Code:
MILK - PLAIN:PAULS:4
BREAD - WHITE:TIPTOP:2
BREAD - WHOLEMEAL:TIPTOP:2
MILO - DRINK:NESTLE:3

zzz
Code:
echo "input search query"
read srch
grep "$srch" xxx > tmp
if [ ! -s tmp ]
then
    echo "unable to find $srch"
else
    grep -v "${srch}" xxx > zxzx
    cat zxzx > xxx
    echo "${srch}:deleted"
fi

bingo SmilieSmilieSmilie, probably an easier way...
# 4  
Old 11-28-2009
Or:
Code:
if grep -q "${1}${2}" $dbfile
then
    echo "Error:not deleted:product does not exist in database"
    exit 1
else
  ...

# 5  
Old 11-28-2009
that didn't work for me
# 6  
Old 11-28-2009
What did not work for you? If your grep does not have a quiet option you could use:
Code:
if grep "${1}${2}" $dbfile >/dev/null
then
    echo "Error:not deleted:product does not exist in database"
    exit 1
else
  ...

# 7  
Old 11-28-2009
hmmm woops
I just retested to make sure I didn't have a typo or something.
and realized the first time round i never actually tested to make sure i could delete an invalid item. It works we just had the if/else wrong way around.

Code:
if grep -q "${1}${2}" $dbfile
then
    #delete the line
else
    echo "cannot delete:"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is it correct? if test grep EOF $a ...

read a if test grep EOF $a then echo yes file else echo no fi (1 Reply)
Discussion started by: iamsumibisht
1 Replies

2. Shell Programming and Scripting

Using grep with test and without using [[ ]]

As an exercise, I'm trying to re-write this code without the compound square brackets, using grep and test. Need to know what to do about the "equal-tilde". #!/bin/bash # test-integer2: evaluate the value of an integer. INT=-5 if +$ ]]; then if ; then echo "INT is zero." else if ; then... (17 Replies)
Discussion started by: Xubuntu56
17 Replies

3. Shell Programming and Scripting

Unable to check if my command succeeded

Hi, Is there a way to check if the openssl part of the below execution returns non zero without freezing the flow of the script ? var1=$(echo | openssl s_client -connect $url 2>/dev/null | openssl x509 -noout -issuer -subject -dates) echo $? Output: 0 while i was expecting the... (2 Replies)
Discussion started by: mohtashims
2 Replies

4. Shell Programming and Scripting

Grep/print/ a test file

cat abc.txt Filename: SHA_AED_Monthly_SNR_20150331.txt.gz Data Format: ASCII with carriage returns and linefeeds Compression: GZIP GZIP Bytes: 36893068 Unzipped Bytes : 613794510 Records: 851310 Record Length: 738 Blocksize: 32472 Filename: SHA_AED_SNR_ChangeLog_20150331.txt.gz Data... (16 Replies)
Discussion started by: dotran
16 Replies

5. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

6. Shell Programming and Scripting

If test grep.... always returns 0 status

Hi all. I am trying to compare and filter two files. I have a bigfile.txt of names and ids and a smallfile.txt of ids only. What I am trying to do is use a while read loop to read the ids in the bigfile and then echo the name and id only if the id exists in the small file. Basically, I'm trying to... (5 Replies)
Discussion started by: jameswatson3
5 Replies

7. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

8. Shell Programming and Scripting

Using grep inside a test

Hi, I want to use grep inside a test statement, but I am getting an error message. Two variables testvarNum=5 testvarNonNum=x echo $testvarNum | grep * The result of this is as follows: 5 However, when I try the following (i.e. to test if the variable is numeric or non-numeric):... (3 Replies)
Discussion started by: dkieran
3 Replies

9. UNIX for Dummies Questions & Answers

Unix grep/test command

Hello, i have a script which checks if the user entered 8 numeric characters in the form of YYYYMMDD (birth date). If the user entered any non numeric characters, an error will be displayed: # Check to see if the 8 characters are all numbers # If not show error essage # And prompt user... (4 Replies)
Discussion started by: netmaster
4 Replies

10. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies
Login or Register to Ask a Question