Solved

Bash string comparison

Posted on 2009-05-20
6
1,482 Views
Last Modified: 2013-12-26
I am trying to find a way to compare the contents of a flat file with a variable.

For example something like this:

if ${STRING} exists in /file.txt then ignore
else
add to file

I am not sure of the exact syntax to compare file contents with a variable so any help on this would be great. Thanks.
0
Comment
Question by:Sam Cohen
  • 4
  • 2
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24433595
if grep -i "${STRING}" /file.txt ; then echo  ignore
else
echo ${STRING} >> /file.txt
fi
0
 
LVL 8

Author Comment

by:Sam Cohen
ID: 24434211
I toyed around with that to see if I could implement it into the pre-existing code and I was running into syntax errors. Here is the code I am working with.

if [ -n "${IPADDR}" ]; then
for IP in ${IPADDR}; do
/bin/logger -p authpriv.warn -t ban "${IP} blocked. Check /etc/hosts.deny"
echo "${IP}  #Blocked : `date +%Y%m%d`" >> /root/test
done
    else
     /bin/logger -p authpriv.warn -t ftp_ban "All Clear"
fi

Open in new window

0
 
LVL 8

Author Comment

by:Sam Cohen
ID: 24434733
still not having any luck :( any suggestions on a better way to accomplish this?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 84

Expert Comment

by:ozo
ID: 24435275
where do you want to implement a comparison into the pre-existing code ?
0
 
LVL 8

Author Closing Comment

by:Sam Cohen
ID: 31583578
I modified my code and got it to work. Thanks!
0
 
LVL 8

Author Comment

by:Sam Cohen
ID: 24435506
actually I got it to work, just needed to modify my code a bit more. Thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now