lundi 6 juillet 2015

if statment to skip loop being ignored. infinite loop Bash script

I am trying to have my script automatically create groups if it runs into a server where it provides an error that the group doesn't exist upon creation attempt.

My issue is when the ID already exists, the script will pull the error: useradd: user 'UserID' already exists this becomes stored in the $check variable. here is my section of code:

echo $CreateID > test
check="$(cat test)"
if [[ $check == "UX: /usr/sbin/useradd: ERROR: $USERNAME is already in use.  Choose another." || $check == "useradd: user '$USERNAME' already exists" ]]; then
    check="";
fi;
case $OS in
    *Linux*)
        check="$(cat test)"
        if [[ $check == "UX: /usr/sbin/useradd: ERROR: $USERNAME is already in use.  Choose another." || $check == "useradd: user '$USERNAME' already exists" ]]; then
    check="";
fi;
        echo $LnxVer
        if [[ $LnxVer == "32-504" || $LnxVer == "32-431" ]]; then
            sed -r 's/.{16}//' test > test1;
            sed -r 's/.{16}$//' test1 > test2;
            NewGroup="$(cat test2)";
        fi
        if [[ $LnxVer == "16" ]]; then
            sed -r 's/.{24}//' test > test1;
            sed -r 's/.{2}$//' test1 > test2;
            NewGroup="$(cat test2)";
        fi
        check="$(cat test)";
        if [[ $check == "UX: /usr/sbin/useradd: ERROR: $USERNAME is already in use.  Choose another." || $check == "useradd: user '$USERNAME' already exists" ]]; then
            check="";
        fi;;

while [[ $check != "" ]]; do

#CreateID="$(cat test)" 
case $OS in
    *Linux*)
        CrPrGrp="$sudo /usr/sbin/groupadd $NewGroup";
        echo "creating $NewGroup group";
        GrpBuild=$(ssh -n -t -t $SERVERNAME "$CrPrGrp");
        echo $CrPrGrp;
        echo $GrpBuild;
        echo "^^^^^^^$NewGroup group created^^^^^^^^^^^";
        echo "\/\/\/\/\/attempting ID create\/\/\/\/\/";
        CreateID=$(ssh -n -t -t $SERVERNAME "$Create");
        echo "$CreateID" > test;
        check="$(cat test)";
        if [[ $check == "UX: /usr/sbin/useradd: ERROR: $USERNAME is already in use.  Choose another." || $check == "useradd: user '"$USERNAME"' already exists" ]]; then
            check="";
        fi;
        if [[ $LnxVer == "32-504" || $LnxVer == "32-431" ]]; then
            sed -r 's/.{16}//' test > test1;
            sed -r 's/.{16}$//' test1 > test2;
            NewGroup="$(cat test2)";
        fi;
        if [[ $LnxVer == "16" ]]; then
            sed -r 's/.{24}//' test > test1;
            sed -r 's/.{2}$//' test1 > test2;
            NewGroup="$(cat test2)";
        fi;;

I no matter what i try, the if statements are being ignored. I have even tried just having

#if [[ $check == "useradd: user 'UserID' already exists" ]]; then
    #   check="";
#fi;`

in place and same result.. cat test does indeed contain that value only. this is not an issue with the files storing the information but rather bash not understanding that the stipulation is true and skipping the while loop.

I hope this all makes sense. I really appreciate any suggestions.

Aucun commentaire:

Enregistrer un commentaire