Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: check if hostname is v-q* how to use wildcards?

Tags:

I have the next code and I need it to echo 1 if the hostname matches with v-qai01 or any other v-q* servers:

if [ `hostname -s` -eq `v-q*` ]; then         echo "1" fi 

Im having several errors:

./run.sh: line 3: v-q*: command not found ./run.sh: line 3: [: v-qai01: unary operator expected 

Any suggestions please?

What if I have the next case?

hostname=`hostname -s`  portalesWildcard=v-*ws* qaiservers={'v-qai01' 'v-qai02'} portales={'t1wsyellar01' }   if [[ ${hostname} = ${qaiservers} ]]; then     echo "yes" fi 

Thanks

like image 792
BoDiE2003 Avatar asked May 10 '11 19:05

BoDiE2003


2 Answers

Use double square brackets and the = operator will accept wildcards:

#!/bin/bash  if [[ $(hostname -s) = v-q* ]]; then     ... fi 

It also has a =~ operator for regex matches when you need more advanced string matching. This would check that the host name also ends with one or more digits:

#!/bin/bash  if [[ $(hostname -s) =~ ^v-q.*[0-9]+$ ]]; then     ... fi 
like image 72
John Kugelman Avatar answered Oct 25 '22 08:10

John Kugelman


you can use the case statement:

case $(hostname -s) in   v-q*) echo yes ;;   *) echo no ;; esac 
like image 25
glenn jackman Avatar answered Oct 25 '22 08:10

glenn jackman