Solved

Bash string comparison

Posted on 2009-05-20
6
1,505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

688 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