?
Solved

Bash string comparison

Posted on 2009-05-20
6
Medium Priority
?
1,524 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 85

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 85

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

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

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

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…
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…
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

840 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