Shell Heredoc containing shell variables

Hello All,

I would like to know if there is a better way to do what is shown below.
Thanks in advance.
Bash:
# Stand alone script extract

set -x

_var1="--- _var1 ---"
_var2="--- _var2 ---"

getcmds1() {
  if [ $# -eq 1 ]; then
    local _fd="$1"
    eval "exec ${_fd}"$'<<\'EOF\'
Function Requirements:
1 The lines in this heredoc contain shell variables, such as ${_var1}, that should not be parsed.
2 The lines are stored via a file descriptor for later procesing.
3 The file descriptor is a function parameter, _fd="$1".
4 The shell variables should not be escaped. i.e. \\${_var2} etc.
5 No external commands should be used. i.e. cat.
6 The heredoc cannot be a file.
EOF
'
  fi
}

getcmds1 4

if { tmpfile1=$(mktemp -t "cmds_$$.") && exec 5> "${tmpfile1}" ; } then

  _cmd1=""
  while :; do
    _ret=0
    read -r _cmd1 <&4 || _ret=$?
    case ${_ret} in
      1) break ;;   # EOF
      0) : ;;       # Valid
      *) break ;;
    esac

    echo "The double quoted line: ${_cmd1}" >&5
    echo "The unquoted line: " ${_cmd1} >&5
    eval "echo The evaluated line: ${_cmd1} >&5"

  done
  exec 5>&-

fi

exec 4<&-
----
Code:
# Output of the temporary file created by the script.

The double quoted line: Function Requirements:
The unquoted line:  Function Requirements:
The evaluated line: Function Requirements:
The double quoted line: 1 The lines in this heredoc contain shell variables, such as ${_var1}, that should not be parsed.
The unquoted line:  1 The lines in this heredoc contain shell variables, such as ${_var1}, that should not be parsed.
The evaluated line: 1 The lines in this heredoc contain shell variables, such as --- _var1 ---, that should not be parsed.
The double quoted line: 2 The lines are stored via a file descriptor for later procesing.
The unquoted line:  2 The lines are stored via a file descriptor for later procesing.
The evaluated line: 2 The lines are stored via a file descriptor for later procesing.
The double quoted line: 3 The file descriptor is a function parameter, _fd="$1".
The unquoted line:  3 The file descriptor is a function parameter, _fd="$1".
The evaluated line: 3 The file descriptor is a function parameter, _fd=.
The double quoted line: 4 The shell variables should not be escaped. i.e. \${_var2} etc.
The unquoted line:  4 The shell variables should not be escaped. i.e. \${_var2} etc.
The evaluated line: 4 The shell variables should not be escaped. i.e. ${_var2} etc.
The double quoted line: 5 No external commands should be used. i.e. cat.
The unquoted line:  5 No external commands should be used. i.e. cat.
The evaluated line: 5 No external commands should be used. i.e. cat.
The double quoted line: 6 The heredoc cannot be a file.
The unquoted line:  6 The heredoc cannot be a file.
The evaluated line: 6 The heredoc cannot be a file.
 
Back
Top