Script shell execution failing escaping problem

62 Views Asked by At

I have been debuging this for one day now without finding the right problem, well what I noticed is that my script dont advance executing the if after sudo podman container inspect nova_libvirt | jq ".[].State.Status" | grep running as it only shows its result and stop after, I was suspecting an escape problem I tried many solutions but in vain here is the script : "

#!/bin/bash

# Check if file does not exist
if [ ! -f "computes_to_delete" ]; then 
  echo "File 'computes_to_delete' not found"
  exit 1
fi

while IFS= read -r compute_to_delete; do
  compute_sh=${compute_to_delete}
  { cat <<EOF
    #!/bin/bash
    echo "Check VNFS Step!"
    echo "The first compute to delete is $compute_to_delete"
    server_name=$(cut -d'.' -f1 <<< "$compute_to_delete")
    echo "server_name is : \$server_name"
    desired_ip=\$(openstack server list -f value -c Networks -c Name | grep -i "\$server_name" | awk -F '[=,]' '{print \$2}')
    echo "desired ip is : \$desired_ip"
    if [ -z "\$desired_ip" ]; then
      echo "Desired IP not found."
      exit 1
    fi

    # Try to SSH into the server    
    ssh -o StrictHostKeyChecking=no heat@\$desired_ip ''' 
        echo "Connected to $desired_ip."
        pwd
        echo \$PWD
        sudo podman container inspect nova_libvirt | jq ".[].State.Status" | grep running
        if [ $? -eq 0 ]; then
            echo "Container nova_libvirt is running."
            sudo podman exec nova_libvirt sh -c '
            if virsh list --all | grep -q -E "^[[:space:]]*[0-9]+"; then
              echo "Error: VMs are still running under nova_libvirt."
              exit 1
            else
              echo "No VMs are running under nova_libvirt. You are all good to remove the compute after!"
              exit 0
            fi   
        else
            echo "Container nova_libvirt is not running."
            exit 1 
        fi ''' || { echo "Failed to connect to \$desired_ip." ; exit 1; }
EOF
  } | sshpass -p ${PWD} ssh -o StrictHostKeyChecking=no stack@${DIRECTOR_IP} "source stackrc && bash -s" >> "check_vnfs.log"
done < "computes_to_delete"

the log file shows: "

Check VNFS Step! The first compute to delete is cpu11.XX.exx server_name is : cpu11 desired ip is : 11.11.11 Connected to . /home/heat /home/heat "running" "

and the sh -x show : "

  • IFS=
  • read -r compute_to_delete
  • compute_sh=cpu11.XX.exx
  • cat
  • sshpass -p ecocenter ssh -o StrictHostKeyChecking=no stack@xx 'source /stackrc && bash -s' ++ cut -d. -f1

Warning: Permanently added '( I m hiding the @' (ECDSA) to the list of known hosts. bash: -c: line 10: syntax error: unexpected end of file

  • IFS=
  • read -r compute_to_delete "

I tried to execute the cript expecting it to advance and to connect with sh to the container in order to show the attached projects , but it never advance rightly to this point

1

There are 1 best solutions below

4
jhnc On

''' is the same as '.

If you indent your code consistently, you will see that your quotes don't start stop where intended:

ssh ... '
    ...
    if ...
        ...
        sudo podman ... sh -c '
            if ...
            else ...
            fi
    else
        ...
    fi
' || ...

A close quote should come after the first fi.

However, single-quotes cannot nest.


Note that multiple levels of expansion are error-prone (eg. $? should probably be \$? as noted by @konsolebox).

You can avoid some of this by splitting in two:

cat <<EOD1
# assign variables here
compute_to_delete=$compute_to_delete
EOD1

cat <<'EOD2'
# everything here is transmitted verbatim
EOD2

However, extra refactoring will be needed to handle the attempted nesting of single-quotes.