Solved

Bash string comparison

Posted on 2009-05-20
6
1,476 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
exchange 2007 3 48
Changing $PATH variable - OS X 2 55
Choosing the right language for new project 8 58
Replace a tag with sed 2 41
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

16 Experts available now in Live!

Get 1:1 Help Now