Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bash string comparison

Posted on 2009-05-20
6
Medium Priority
?
1,521 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Suggested Courses

636 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