简体   繁体   中英

Bash check with multiple conditions

I have a script like this that checks 1 snapshot repository in Elastic for the success / failure of the last backup

#!/bin/bash
project=emirates ( 3 new projects are added here )

_BACKUP_STATUS=$(curl -s -X GET "10.0.45.101:9200/_cat/snapshots/$project?v&s=id&pretty" | tail -1 | awk '{ print $2 }')

[[ $_BACKUP_STATUS = "SUCCESS" ]] && echo elasticsearch_backup_status{project='"$project"', env='"prod"', zone='"dubai"'} 0 > /var/lib/node_exporter/textfile_collector/elasticsearch_backup_status.prom || echo elasticsearch_backup_status{project='"$project"', env='"prod"', zone='"dubai"'} 1 > /var/lib/node_exporter/textfile_collector/elasticsearch_backup_status.prom

but the fact is that I have 3 more projects etihad, flydubai, airarabia, how then to check every 3 projects?

i have a solution is to change $project variable to $1, then call this script from another script like that

for company in etihad flydubai airarabia; do
    /path/to/script.sh $company
done

but i want to do this using only 1 script

You have it all; instead of looping over the array and calling a script - execute that script content.

#!/bin/bash
PROJECTS=(emirates emirates2 emirates3) 

for project in ${PROJECTS[@]}; do
  _BACKUP_STATUS=$(curl -s -X GET "10.0.45.101:9200/_cat/snapshots/$project?v&s=id&pretty" | tail -1 | awk '{ print $2 }')

  [[ $_BACKUP_STATUS = "SUCCESS" ]] && echo elasticsearch_backup_status{project='"$project"', env='"prod"', zone='"dubai"'} 0 > /var/lib/node_exporter/textfile_collector/elasticsearch_backup_status.prom || echo elasticsearch_backup_status{project='"$project"', env='"prod"', zone='"dubai"'} 1 > /var/lib/node_exporter/textfile_collector/elasticsearch_backup_status.prom
done

Now you can change PROJECTS="$@" and call your script with the list of projects instead of hard-coding them inside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM