0

Why does the status variable not take the value of the cat command as written, knowing that the cat command before the right content? I have been looking for some hours now : check the lines with (*)

if [ ! -f "node.txt" ]; then
     echo " File computes_to_delete not found"
     exit 1;
fi

while read -r compute_to_delete; do
    compute_to_delete=${compute_to_delete}
    compute_sh=${compute_to_delete}.sh
    { cat << EOF;} > "$compute_sh"
#!/bin/bash
echo "compute to be deleted"
echo  $compute_to_delete
openstack compute service list
openstack compute service set $compute_to_delete nova-compute --enable
openstack compute service list --long -c Binary -c Host -c Status -f json | jq -r '.[] | select(.Host == "$compute_to_delete" ) | .Status' > status.txt
(*) cat status.txt
(*) STATUS=$(< status.txt)
echo "$STATUS"

if [ "$STATUS" == "enabled" ]; then


fi

openstack server list --host $compute_to_delete --all-projects
EOF

done < computes_hosts.txt

As suggested in the comments, I've added backslashes before the concerned variable and now its working fine

if [ ! -f "node.txt" ]; then
     echo " File computes_to_delete not found"
     exit 1;
fi

while read -r compute_to_delete; do
    compute_to_delete=${compute_to_delete}
    compute_sh=${compute_to_delete}.sh
    { cat << EOF;} > "$compute_sh"

#!/bin/bash
echo "compute to be deleted"
echo  $compute_to_delete
openstack compute service list
openstack compute service set $compute_to_delete nova-compute --enable
openstack compute service list --long -c Binary -c Host -c Status -f json | jq -r '.[] | select(.Host == "$compute_to_delete" ) | .Status' > status.txt
 cat status.txt
 STATUS=\$(< status.txt)
echo "\$STATUS"

if [ "\$STATUS" == "enabled" ]; then


fi

openstack server list --host $compute_to_delete --all-projects
EOF

done < computes_hosts.txt
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • use `<< 'EOF'` so that variables in the here-doc won't be expanded. – Barmar Aug 16 '23 at 23:13
  • @Barmar I have just tried but no it still not getting right value, and also after adding the ' ' even the cat is no more showing the content – Madiha Harifi Aug 16 '23 at 23:30
  • 1
    It looks like you have a problem. You want `$compute_to_delete` to be expanded, but you don't want `$STATUS` to be expanded. So go back to the original `<<`, but put backslash before the `$` that should not be expanded. – Barmar Aug 16 '23 at 23:34
  • 1
    E.g. `STATUS=\$(< status.txt)` and `echo "\$STATUS"` – Barmar Aug 16 '23 at 23:35
  • 1
    Both methods are explained in the linked question. – Barmar Aug 16 '23 at 23:35
  • 1
    If you have modified the code, please update your question and **add** (don't replace!) the new code which, despite its corrections, does not work. Also don't put weird `(*)` into your code. If you want to provide inline explanation, do it as a comment (`#....`). – user1934428 Aug 17 '23 at 08:37
  • done @user1934428 – Madiha Harifi Aug 17 '23 at 14:21

0 Answers0