Home > front end >  checks both word with dash and underscore (-/_)
checks both word with dash and underscore (-/_)

Time:09-17

Our server names either have a dash (-) or underscore (_)

file.txt consists of:

irl-iv-host01
irl-vm-host02
irl-rv-host01
irl-sd-host01

Objective is to check if the server exists and then run the next command.

So this command right here is where we check if the server exist on the list of zones.

var1=`ssh [email protected] cfgactvshow | grep -i "$host" | awk '{print $2}'`

However, there are instances that the host configured on the list of zones are different, sometimes its irl-iv-host01 or irl_iv_host01 or irl-iv_host01 or even irl_iv-host01

so what i want is to check if host irl-iv-host01 is on the list of zones whether it's name is any of these irl-iv-host01, irl_vm_host01, irl-iv_host01, irl_iv-host01

and then use it for another command that checks the zonename which is

ssh [email protected] zoneshow "$var1"

Now, if in the event that during the grep process the result is multiple, i guess or maybe it will be stored to another variable which we can also use like the sample below.

Example if multiple output:

ssh [email protected] cfgactvshow | grep -i "$host" | awk '{print $2}'
zone1_irl-iv-host01
zone2_irl-iv-host01
zone3_irl-iv-host01

ssh [email protected] zoneshow zone1_irl-iv-host01
ssh [email protected] zoneshow zone2_irl-iv-host01
ssh [email protected] zoneshow zone3_irl-iv-host01

The question is, is there a simpler way to do it?

I just feel i'm running around the bush

while read -r host;
        do
        withunderscore=`echo $host | sed 's/-/_/g'`
        var1=`ssh [email protected] command | grep "$host"`
        var2=`ssh [email protected] command | grep "$withunderscore"`
        
        if var1 is not empty then run the command but if it is, test var2. 
        If var2 is not empty then run the command but if it is, echo "both vars are empty.

done < file.txt 

I would appreciate any help. thank you.

CodePudding user response:

Try to change - and _ to regex [-_]:

while read -r host; do
    var=$(ssh [email protected] command | grep -E "${host//[-_]/\[_-\]}")
done < file.txt

CodePudding user response:

You can do something like this:

# grab output of ssh [email protected] cfgactvshow only once
hostdata="$(ssh [email protected] cfgactvshow)"

while read -r host; do
   echo "processing host: $host"

   grep -F "${host//_/-}" <<< "$hostdata" | while read -r var; do
      # run ssh check
      ssh [email protected] zoneshow "$var"
   done

done < file.txt 

What it does:

  1. Runs ${host//_/-} to replace all _ with - before running grep -F with $host (variable with all hyphens only)
  2. Uses output of first ssh command
  3. Loops through output of cfgactvshow command to run next zoneshow command

CodePudding user response:

cat file.txt | tr '_' '-' > f2
mv f2 file.txt

That will change all of the underscores to hyphens.

while read -r host;
    do
    ssh [email protected] cfgactvshow | grep -i "$host" | awk '{print $2}'
    done < file.txt
  • Related