0

I am running a ksh script from root where I switch (su) to different users and execute the commands. But, some reason EOF does not. Here is part of my code.

This part of the code does not work su - $instance <<'EOF': it does not pass the $instance

instgroup=`id $instance | awk {'print $2'} | sed 's/[^ ]*( *//' | sed 's/.$//'`
db2instance=`ps -eaf | grep db2sysc |grep -v grep | awk '{print $1}' | sort
for instance in ${db2instance}; do
     echo "$instance"
     su - $instance <<'EOF'
     echo "user -" ${instance}
     echo "Checking all DB paths for Databases in $instance"
     echo ""
     $HOME/sqllib/db2profile
     for dbname in `db2 list db directory | grep -p Indirect | grep alias | awk '{print $NF}'`
     do
       echo "...."
     done
  EOF
done
roaima
  • 107,089
  • 14
  • 139
  • 261
  • `<<'EOF'` is the opening of a here document. Where is the closing `EOF` on a line by itself? – Sotto Voce Nov 04 '22 at 20:29
  • I have EOF in my code but when I execute the script, it returns $instance instead of like db2inst1, db2inst2 and etc. – user3590915 Nov 04 '22 at 21:38
  • `EOF` needs to be **un**quoted if you want variable expansion in the heredoc to take place - see for example [passing and setting variables in a heredoc](https://unix.stackexchange.com/questions/405250/passing-and-setting-variables-in-a-heredoc) – steeldriver Nov 04 '22 at 23:23

1 Answers1

2

A single-quoted heredoc is treated as a string literal. Consider this example,

hw='hello world'

echo 'Attempt 1'
nl <<EOF
Good morning
My phrase today is $hw
That is all
EOF

echo 'Attempt 2'
nl <<'EOF'
Good morning
My phrase today is $hw
That is all
EOF

Output

Attempt 1
     1  Good morning
     2  My phrase today is hello world
     3  That is all
Attempt 2
     1  Good morning
     2  My phrase today is $hw
     3  That is all
roaima
  • 107,089
  • 14
  • 139
  • 261