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.
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 ----------
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
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)
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)
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)
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)
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)
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)
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)
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)