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
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
you can use the case
statement:
case $(hostname -s) in v-q*) echo yes ;; *) echo no ;; esac
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With