LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2012, 02:54 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
bash string comparisons wildcard


So I have three instances - bincasper32, bincasper41, bincasper42.
They all need to be mapped to port 23471
I wrote a small script that scans the config files after they
are configured, with a few functions.

this works fine
Code:
function ports_bincasper
{
if [[ "$application" == "bincasper41" ]] && [[ "$port" -ne "23471" ]] ;
        then
        echo "               The port to $account is NOT configured correctly"
fi
}
But I would rather not have three seperate functions.
I tried putting a wildcard in the application comparison, but it does not work.
Is there a way for me to do this?
Code:
function ports_bincasper
{
if [[ "$application" == "bincasper*" ]] && [[ "$port" -ne "23471" ]] ;
        then
        echo "               The port to $account is NOT configured correctly"
fi
}
 
Old 08-08-2012, 02:59 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Code:
#!/bin/bash

hosts="bincasper32 bincasper41 bincasper42"

for host in $hosts
do
if [[ "$application" == "$host" ]] && [[ "$port" -ne "23471" ]] ;
        then
        echo "               The port to $account is NOT configured correctly"
fi
BAM!
 
1 members found this post helpful.
Old 08-08-2012, 03:20 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
KAPOW !!!
it worked,
 
Old 08-09-2012, 09:07 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or you could use either parameter substitution or the regex operator:
Code:
if [[ $application =~ bincasper ]] && (( port != 23471 ))

if [[ ${application%%[0-9]*} ==  ]] && (( port != 23471 ))
With the first option you can extend the regex to say it must start (^) with "bincasper" and that it is followed by zero or more (*), one or more (+) or even by exactly 2 ({2}) digits ([0-9] or [[:digit:]]
 
1 members found this post helpful.
Old 08-10-2012, 12:42 AM   #5
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
hey - the =~ works great - ( that is what i was originally looking for)

I do not understand the second example - especially the %%

Last edited by casperdaghost; 08-10-2012 at 02:04 AM.
 
Old 08-10-2012, 03:30 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Search for %% on the following page:

http://tldp.org/LDP/abs/html/paramet...stitution.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Multiple string variable comparisons in an if statement in bash buee Linux - Software 4 01-02-2019 08:21 AM
Bash change the items in comparisons with simple front end geeyathink Programming 3 03-04-2008 09:53 PM
date comparisons and increments in bash elinenbe Programming 5 10-19-2007 11:40 AM
string comparisons ricmon Programming 18 06-12-2007 10:31 AM
Perl string comparisons Xris718 Programming 5 04-03-2005 10:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration