0

I have below script which run successfully if i use #!/usr/bin/env bash.

The same script is getting unexpected EOF while looking for matching " error while using #!/bin/ksh.

Why is that?

#!/usr/bin/env bash
#--------------------
# Command variables
#--------------------
ECHO=/usr/bin/echo
SED=/usr/bin/sed
EXPR=/usr/bin/expr
EGREP=/usr/bin/egrep
DATE=/usr/bin/date
CUT=/usr/bin/cut

#--------------------
# Local variables
#--------------------
shopt -s extglob
SHELLNAME=$(basename $0)
GRANT_RAW_AMT=''
GRANT_AMT=0
GRANT_FILE=''
FILE_BN=''
COMPANY=''
EMAIL_GRANT=''
EMAIL_RECIPIENTS=$(${ATAS_SHELL}/get_global_constant.sh RESP RESP_CASH_RECIEPT)
EMAIL_RETURN=$(${ATAS_SHELL}/get_global_constant.sh RESP CASH_RECEIPT_SENDER)
RESP_DIR=${ATAS_RESP}/$(${ATAS_SHELL}/get_global_constant.sh RESP RESPDIR)
RESP_LOG=${ATAS_RESP}/log/${0##*/}.$(/usr/bin/date +%y%m%d).$(/usr/bin/date +%H%M).log

#--------------------------------------------
#  Determine the amount of grant for the company
#--------------------------------------------
  GRANT_RAW_AMT=000078753603
  unpadded_amt=${GRANT_RAW_AMT##+(0)} # trim leading 0's -- otherwise a value that starts with
                              # 0s can be treated as octal rather than decimal.
  printf -v GRANT_AMT '%d.%02d' "$(( unpadded_amt / 100 ))" "$(( unpadded_amt % 100 ))"
  EMAIL_GRANT=${EMAIL_GRANT}${COMPANY}'\t$'${GRANT_AMT}'\n'
${ECHO} ${EMAIL_BODY1}${EMAIL_GRANT}
#--------------------------------------------
#  End of Program
#--------------------------------------------
echo >> ${RESP_LOG}
${ECHO} "${SHELLNAME} completed"
exit 0
  • 3
    Because ksh and bash use 2 different languages. It would probably run if it was written for sh. You have to find what in this script are bashisms. – Pierre-Alain TORET Mar 8 '18 at 14:44
4

shopt -s extglob is bash-specific (shopt alone is bash-specific, bash is the only shell to have two sets of options)

The extglob option in bash enables support for the ksh extended globs. It's not needed in ksh, though note that in ksh those globs are not recognised when used in variables.

printf -v is a bash extension. It's not support and not needed in ksh93 because there, command substitution doesn't fork a subshell. So instead of:

printf -v var ...

use

var=$(printf ...)

or in ksh93:

var=${ printf ...;}

if arguments to printf are supposed to make changes to the environment (like in ${foo=bar} or $((n++))) . (though note that it strips trailing newline characters).

Also note that ksh93 does support floating point arithmetics, contrary to bash, so you don't need to resort to this kind of hack.

Other than that, I can't see anything in there that could explain the error message you're getting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.