dimanche 6 novembre 2016

How to check and exit when entire condition is met in for loop bash script?

I m stuck in a condition where i have a for loop and then if condition inside it. I want my loop should work as it is prints the value and after for loop is over. Checks if it has ever gone to else condition if yes then exit. My for loop and if condition works fine but i dont know how to check if it has gone in else condition and then exit. Basically my aim is to print everything if and else which works but after loop is done. Goes and check back whether gone to else anytime then only exit. Script is in BASH.

Script is :

#!/bin/sh

env=$1

if [ -n $env ]; then
    echo "Checking env.."
else
        echo "You need to add env ..."
        exit
fi


echo " Get the alias "
curl -s -XGET 'http://search.'$env'.com:9200/_cat/aliases' | grep current > all-aliases.txt


for index in `cat all-aliases.txt | awk '{print $1}'`;
 do
echo $index;

occur=$(grep -o ^"$index"  all-aliases.txt | wc -l);
echo $occur;
if [ $occur -gt 1 ];
then
echo  $index "alias has more than one index , remove one "
exit 1
else
echo   $index " alias have just one index "

fi
done

I want exit condition after the loop because i m trying to run the exit in Jenkins conditional plugin.. Where loop is executed first and exit in condition after loop is done.

Aucun commentaire:

Enregistrer un commentaire