0

I have a function (notify_dba) that with two string args, a header and a body. (correction) Works well from shell prompt, but not in cron Err, doesn't work in either shell or cron (but should...?):

 53 notify_dba "${FRIENDLY_SERVER_NAME}: New ORA errors in AlertLog" echo ${
 54 "Please check full log in ${ALERT_LOG_LOCATION}.
 55 =====================================================
 56 `grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
 57 =====================================================
 58 "}

Getting the error:

Wed Jan 16 06:00:01 PST 2019
LAST_LINE_FROM_ARCHIVE=449843
LAST_LINE_FROM_TEMP= 452866
/u01/app/oracle/admin/chk_alertlog.sh: line 53: ${
"Please check full log in ${ALERT_LOG_LOCATION}.
=====================================================
`grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
=====================================================
"}: bad substitution
  • 1
    Ok, I was wondering how could it possibly work in a shell... But you just made your edit. The unquoted ${ triggers parameter substitution, which gives you the bad substitution error. Why the curly braces surrounding your block? They seem just wrong there. – fra-san Jan 16 at 19:43
  • lol, the shell hit a condition and this was the first time this block of code hit... I'll try without the curly braces - the thought was that being a multi-line string argument that I might have to encapsulate with squirrelies .. – Dennis Jorgenson Jan 16 at 20:05
  • Seems to have done the trick. Thanks a bunch! – Dennis Jorgenson Jan 16 at 20:48
  • Glad to hear that. Even if I'm late, I'm posting an answer with some examples on handling multi-line and non-fixed text in scripts. – fra-san Jan 16 at 21:32
  • Pleased to see you're already sorted. In general, please don't post code with line numbers. It adds work in cutting and pasting the example code. – roaima Jan 18 at 8:39
1

As pointed out in a comment to your question, the error you see is due to the unquoted ${, which triggers parameter expansion (see "Parameter Expansion" in section "EXPANSION" of man bash).

That said, you can of course pass around a multi-line text block. You have just to quote it properly:

If it is static, single quotes are ok:

$ mlblock='line 1
> line 2'
$ printf '%s\n' "$mlblock"
line 1
line 2

If you want it to be built at execution time, you have to use double quotes, which preserve the special meaning of $, ` and \, allowing, among other things, parameter expansion and command substitution:

$ text="Sample    text"
$ mlblock="$text
> date: $(date)
> end"
$ printf '%s\n' "$mlblock"
Sample    text
date: Wed Jan 16 22:18:33 CET 2019
end

Note that expansions appearing anywhere in a string enclosed in double quotes are themselves quoted. Thus, their spacing is preserved (e.g. Sample text).

The most readable way to pass a complex string to a function is probably to use a variable:

$ function notify_dba () {
    printf '%s\n' "$1"    # Print header
    printf '%s\n' "$2"    # Print body
}
$ notify_dba 'Header' "$mlblock" 
Header
Sample    text
date: Wed Jan 16 22:21:14 CET 2019
end

Note that the second argument has to appear in double quotes because: 1) if unquoted, it would be expanded by the shell and, when resulting in more than one word, be translated into more than one arguments to the function; 2) if surrounded by single quotes, it would be interpreted literally as the string $mlblock.

Alternatively you can, of course, build the argument string as you call your function:

$ notify_dba 'Header' "Multi
> line body
> date: $(date)
> end"
Header
Multi
line body
date: Wed Jan 16 22:29:13 CET 2019
end

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.